C# Binarywriter

The BinaryWriter class in C# is employed for composing binary data into a stream. This class is located within the System.IO namespace and provides functionality for encoding and writing strings into the stream with a specified encoding format.

C# BinaryWriter Example

Let's explore a basic illustration of the BinaryWriter class that is responsible for storing data in a .dat file.

Example

using System;
using System.IO;
namespace BinaryWriterExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "e:\\binaryfile.dat";
            using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
            {
                writer.Write(2.5);
                writer.Write("this is string data");
                writer.Write(true);
            }
            Console.WriteLine("Data written successfully...");  
        }
    }
}

Output:

Output

Data written successfully...

Input Required

This code uses input(). Please provide values below: