How to use C# BinaryWriter class?

The BinaryWriter class in C# is used to write binary data to a stream in a binary format. It is part of the System.IO namespace and provides methods to write primitive data types like integers, strings, doubles, and booleans directly to a stream as binary data.

Unlike text-based writing, BinaryWriter stores data in its native binary representation, making it more efficient for storage and faster to read back using BinaryReader.

Syntax

Following is the syntax for creating a BinaryWriter instance −

BinaryWriter writer = new BinaryWriter(stream);

Following is the syntax for writing data using BinaryWriter −

writer.Write(value);  // where value can be int, string, double, etc.

Common Methods

The BinaryWriter class provides several methods to write different data types −

  • Write(int) − Writes a 32-bit integer

  • Write(double) − Writes a double-precision floating-point value

  • Write(string) − Writes a length-prefixed string

  • Write(bool) − Writes a Boolean value

  • Write(byte[]) − Writes a byte array

BinaryWriter Data Flow C# Data int, string, double BinaryWriter Converts to binary File/Stream Binary format Data is stored in compact binary format Use BinaryReader to read it back

Writing and Reading Binary Data

Example

using System;
using System.IO;

class Program {
   static void WriteData() {
      using (BinaryWriter writer = new BinaryWriter(File.Open("data.bin", FileMode.Create))) {
         writer.Write(42);           // int
         writer.Write(37.8);         // double  
         writer.Write("Hello");      // string
         writer.Write(true);         // bool
      }
   }
   
   static void ReadData() {
      using (BinaryReader reader = new BinaryReader(File.Open("data.bin", FileMode.Open))) {
         Console.WriteLine("Integer: " + reader.ReadInt32());
         Console.WriteLine("Double: " + reader.ReadDouble());
         Console.WriteLine("String: " + reader.ReadString());
         Console.WriteLine("Boolean: " + reader.ReadBoolean());
      }
   }
   
   public static void Main() {
      WriteData();
      ReadData();
   }
}

The output of the above code is −

Integer: 42
Double: 37.8
String: Hello
Boolean: True

Writing Different Data Types

Example

using System;
using System.IO;

class Program {
   public static void Main() {
      using (MemoryStream stream = new MemoryStream()) {
         using (BinaryWriter writer = new BinaryWriter(stream)) {
            writer.Write((byte)255);
            writer.Write((short)1000);
            writer.Write(3.14159f);
            writer.Write(DateTime.Now.ToBinary());
            
            Console.WriteLine("Data written to memory stream");
            Console.WriteLine("Stream length: " + stream.Length + " bytes");
         }
         
         // Read back the data
         stream.Position = 0;
         using (BinaryReader reader = new BinaryReader(stream)) {
            Console.WriteLine("Byte: " + reader.ReadByte());
            Console.WriteLine("Short: " + reader.ReadInt16());
            Console.WriteLine("Float: " + reader.ReadSingle());
            Console.WriteLine("DateTime: " + DateTime.FromBinary(reader.ReadInt64()));
         }
      }
   }
}

The output of the above code is −

Data written to memory stream
Stream length: 19 bytes
Byte: 255
Short: 1000
Float: 3.14159
DateTime: 12/25/2024 4:21:33 PM

BinaryWriter vs StreamWriter

BinaryWriter StreamWriter
Writes data in binary format Writes data as text
More efficient storage Human-readable output
Requires BinaryReader to read Can be read with any text editor
Preserves exact data types All data becomes text

Conclusion

The BinaryWriter class provides an efficient way to write primitive data types to streams in binary format. It is ideal for storing structured data compactly and should be paired with BinaryReader for reading the data back while preserving the original data types.

Updated on: 2026-03-17T07:04:35+05:30

266 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements