BinaryReader Class

System.IO

Provides a means to read primitive data types from a stream in binary format.

Syntax

public class BinaryReader : IDisposable

Remarks

The BinaryReader class supports reading various primitive data types (like boolean, char, byte, short, int, long, float, and double) from a stream. It also supports reading strings. The data can be written to a stream using the BinaryWriter class.

When you create an instance of BinaryReader, you must provide a stream that is readable. The BinaryReader is not thread-safe. If you need to perform operations on the stream from multiple threads, you must synchronize access to the stream.

BinaryReader reads data based on the encoding specified when the BinaryReader is instantiated. If no encoding is specified, the default is UTF-8.

Constructors

  • BinaryReader(Stream input) public BinaryReader(Stream input)

    Initializes a new instance of the BinaryReader class using UTF-8 encoding and a buffer size of 4096 bytes.

  • BinaryReader(Stream input, Encoding encoding) public BinaryReader(Stream input, Encoding encoding)

    Initializes a new instance of the BinaryReader class using the specified encoding and a buffer size of 4096 bytes.

  • BinaryReader(Stream input, Encoding encoding, bool leaveOpen) public BinaryReader(Stream input, Encoding encoding, bool leaveOpen)

    Initializes a new instance of the BinaryReader class using the specified encoding and optionally leaves the stream open after the BinaryReader object is disposed.

Methods

  • ReadBoolean() public virtual bool ReadBoolean()

    Reads a Boolean value from the current stream and advances the position in the stream by one byte.

  • ReadByte() public virtual byte ReadByte()

    Reads a byte from the current stream and advances the position in the stream by one byte.

  • ReadBytes(int count) public virtual byte[] ReadBytes(int count)

    Reads the specified number of bytes from the current stream and advances the position in the stream by that number of bytes.

  • ReadChar() public virtual char ReadChar()

    Reads the next character from the current stream and advances the current position accordingly.

  • ReadChars(int count) public virtual char[] ReadChars(int count)

    Reads the specified number of characters from the current stream and advances the current position accordingly.

  • ReadDecimal() public virtual decimal ReadDecimal()

    Reads a decimal value from the current stream and advances the position in the stream by sixteen bytes.

  • ReadDouble() public virtual double ReadDouble()

    Reads an 8-byte floating-point value from the current stream and advances the position in the stream by eight bytes.

  • ReadInt16() public virtual short ReadInt16()

    Reads a 2-byte signed integer from the current stream and advances the position in the stream by two bytes.

  • ReadInt32() public virtual int ReadInt32()

    Reads a 4-byte signed integer from the current stream and advances the position in the stream by four bytes.

  • ReadInt64() public virtual long ReadInt64()

    Reads an 8-byte signed integer from the current stream and advances the position in the stream by eight bytes.

  • ReadSByte() public virtual sbyte ReadSByte()

    Reads a signed byte from the current stream and advances the position in the stream by one byte.

  • ReadSingle() public virtual float ReadSingle()

    Reads a 4-byte floating-point value from the current stream and advances the position in the stream by four bytes.

  • ReadString() public virtual string ReadString()

    Reads a string from the current stream. Reads a length-prefixed string, which means that the text is first read from the current stream.

  • ReadUInt16() public virtual ushort ReadUInt16()

    Reads a 2-byte unsigned integer from the current stream and advances the position in the stream by two bytes.

  • ReadUInt32() public virtual uint ReadUInt32()

    Reads a 4-byte unsigned integer from the current stream and advances the position in the stream by four bytes.

  • ReadUInt64() public virtual ulong ReadUInt64()

    Reads an 8-byte unsigned integer from the current stream and advances the position in the stream by eight bytes.

Example