How to use C# BinaryReader class?

The BinaryReader class in C# is used to read binary data from a stream in a specific encoding. It provides methods to read various data types like integers, doubles, strings, and byte arrays from binary files or streams.

The BinaryReader class is located in the System.IO namespace and is commonly used for reading binary files, network streams, or any binary data format.

Syntax

Following is the syntax for creating a BinaryReader instance −

BinaryReader reader = new BinaryReader(stream);

Following is the syntax for reading different data types −

int value = reader.ReadInt32();
double value = reader.ReadDouble();
string text = reader.ReadString();
byte[] bytes = reader.ReadBytes(count);

Common Methods

The BinaryReader class provides several methods to read different data types −

Method Description
ReadBoolean() Reads a Boolean value from the stream
ReadByte() Reads a single byte from the stream
ReadInt32() Reads a 4-byte signed integer
ReadDouble() Reads an 8-byte floating point value
ReadString() Reads a string from the stream
ReadBytes(int) Reads specified number of bytes

Using BinaryReader to Read from File

The following example demonstrates writing binary data to a file and then reading it back using BinaryReader −

using System;
using System.IO;

class Program {
    static void WriteMe() {
        using (BinaryWriter w = new BinaryWriter(File.Open("data.dat", FileMode.Create))) {
            w.Write(25.9);
            w.Write("DEMO DATA");
            w.Write(42);
            w.Write(true);
        }
    }

    static void ReadMe() {
        using (BinaryReader r = new BinaryReader(File.Open("data.dat", FileMode.Open))) {
            Console.WriteLine("Double Value: " + r.ReadDouble());
            Console.WriteLine("String Value: " + r.ReadString());
            Console.WriteLine("Integer Value: " + r.ReadInt32());
            Console.WriteLine("Boolean Value: " + r.ReadBoolean());
        }
    }

    static void Main(string[] args) {
        WriteMe();
        ReadMe();
    }
}

The output of the above code is −

Double Value: 25.9
String Value: DEMO DATA
Integer Value: 42
Boolean Value: True

Using BinaryReader with MemoryStream

BinaryReader can also work with memory streams for reading binary data from byte arrays −

using System;
using System.IO;

class Program {
    static void Main(string[] args) {
        byte[] data;
        
        // Write binary data to memory stream
        using (MemoryStream ms = new MemoryStream()) {
            using (BinaryWriter writer = new BinaryWriter(ms)) {
                writer.Write(100);
                writer.Write(3.14159);
                writer.Write("Hello Binary");
            }
            data = ms.ToArray();
        }
        
        // Read binary data from memory stream
        using (MemoryStream ms = new MemoryStream(data)) {
            using (BinaryReader reader = new BinaryReader(ms)) {
                Console.WriteLine("Integer: " + reader.ReadInt32());
                Console.WriteLine("Double: " + reader.ReadDouble());
                Console.WriteLine("String: " + reader.ReadString());
            }
        }
    }
}

The output of the above code is −

Integer: 100
Double: 3.14159
String: Hello Binary

Reading Byte Arrays

BinaryReader provides methods to read specific numbers of bytes or all remaining bytes −

using System;
using System.IO;

class Program {
    static void Main(string[] args) {
        byte[] originalData = {65, 66, 67, 68, 69, 70};
        
        using (MemoryStream ms = new MemoryStream(originalData)) {
            using (BinaryReader reader = new BinaryReader(ms)) {
                // Read first 3 bytes
                byte[] firstThree = reader.ReadBytes(3);
                Console.WriteLine("First 3 bytes: " + string.Join(", ", firstThree));
                
                // Read remaining bytes
                byte[] remaining = reader.ReadBytes((int)(ms.Length - ms.Position));
                Console.WriteLine("Remaining bytes: " + string.Join(", ", remaining));
            }
        }
    }
}

The output of the above code is −

First 3 bytes: 65, 66, 67
Remaining bytes: 68, 69, 70

How It Works

BinaryReader reads data from the underlying stream in the same order it was written by BinaryWriter. The data types must be read in the exact sequence they were written to ensure proper deserialization. The reader maintains a position pointer that advances as data is read.

BinaryReader Process Binary File BinaryReader Typed Data Objects Stream of bytes Reads & converts int, string, double, etc.

Key Rules

  • Always read data in the same order it was written using BinaryWriter.

  • Use using statements to ensure proper disposal of streams and readers.

  • Handle EndOfStreamException when reading beyond the available data.

  • BinaryReader works with any stream type: FileStream, MemoryStream, NetworkStream, etc.

Conclusion

The BinaryReader class in C# provides an efficient way to read binary data from streams. It offers type-safe methods to read various data types and works seamlessly with BinaryWriter for binary file operations and data serialization scenarios.

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

245 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements