BinaryReader Class

Namespace: System.IO

Provides a convenient way to read primitive data types from a stream. This class cannot be inherited.

Introduction

The BinaryReader class in the System.IO namespace allows you to read binary data from a stream in a specific format. It supports reading various primitive data types such as boolean, byte, character, double, short, integer, long, sbyte, float, string, and unsigned short, integer, and long. It's particularly useful when dealing with custom binary file formats or network protocols.

How to Use

You typically create a BinaryReader by passing an existing stream (like a FileStream) to its constructor. You can then use its methods to read data sequentially.

using System.IO; // Assuming 'stream' is an open stream (e.g., from File.OpenRead) using (BinaryReader reader = new BinaryReader(stream)) { int age = reader.ReadInt32(); string name = reader.ReadString(); double score = reader.ReadDouble(); // ... read other data types }

Properties

Name Description
BaseStream Gets the underlying stream.
_stream Internal stream object. (Not public API)

Methods

BinaryReader offers a comprehensive set of methods for reading various data types:

Read Methods

Method Signature Description
ReadBoolean() Reads a one-byte unsigned integer from the current stream and converts it to a Boolean.
ReadByte() Reads a byte from the current stream.
ReadSByte() Reads an 8-byte signed integer from the current stream and converts it to an SByte.
ReadBytes(int count) Reads a specified number of bytes from the current stream and advances the position within the stream by the number of bytes read.
ReadChar() Reads the next character from the current stream and advances the current position according to the Char's encoding.
ReadChars(int count) Reads a specified number of characters from the current stream and advances the current position according to the Char's encoding.
ReadDecimal() Reads an 8-byte unsigned integer from the current stream and converts it to a Decimal.
ReadDouble() Reads an 8-byte floating-point number from the current stream and converts it to a Double.
ReadInt16() Reads a two-byte signed integer from the current stream and converts it to an Int16.
ReadUInt16() Reads a two-byte unsigned integer from the current stream and converts it to a UInt16.
ReadInt32() Reads a four-byte signed integer from the current stream and converts it to an Int32.
ReadUInt32() Reads a four-byte unsigned integer from the current stream and converts it to a UInt32.
ReadInt64() Reads an eight-byte signed integer from the current stream and converts it to an Int64.
ReadUInt64() Reads an eight-byte unsigned integer from the current stream and converts it to a UInt64.
ReadSingle() Reads a four-byte floating-point number from the current stream and converts it to a Single.
ReadString() Reads a UTF-8 encoded string from the current stream. This method reads from the current stream until the end is reached, and returns data as a string.

Other Methods

Method Signature Description
Close() Closes the current BinaryReader and the underlying stream.
Dispose() Releases the unmanaged resources that are used by the current instance of the BinaryReader class and optionally releases the managed resources.

See Also