Skip to Main Content
C# in a Nutshell
book

C# in a Nutshell

by Ben Albahari, Ted Neward, Peter Drayton
March 2002
Intermediate to advanced content levelIntermediate to advanced
864 pages
31h 8m
English
O'Reilly Media, Inc.
Content preview from C# in a Nutshell

Name

BinaryReader

Synopsis

This class allows you to read data from a Stream. When using a BinaryReader, the data represented by a Stream is regarded as a binary format, and bits are merely read from the stream and copied into the appropriate types. The methods prefixed with Read() allow you to grab data of a specific type from the front of the Stream and advance the current position. The next table shows how many bytes each of these methods reads in from a stream.

MethodBytes read
Read variable
ReadBoolean 1
ReadByte 1
ReadBytes variable
ReadChar 2
ReadChars variable
ReadDecimal 16
ReadDouble 8
ReadInt16 2
ReadInt32 4
ReadInt64 8
ReadSByte 1
ReadSingle 4
ReadString variable
ReadUInt16 2
ReadUInt32 4
ReadUInt64 8

ReadString() uses the current encoding of the BinaryReader , which can be set when you call the constructor. Strings are prefixed with their length. PeekChar() allows you to look at the first character of a stream (a System.Char, which is two bytes) without advancing the position of the Stream. Because a binary reader may hold on to resources that should be freed when not needed, the BinaryReader must be closed using Close() or by wrapping it in a using block (which calls the protected Dispose() method to do the cleanup at the end of the using block).

public class BinaryReader : IDisposable {
// Public Constructors
   public method BinaryReader(Stream input);  
   public method BinaryReader(Stream input, 
        System.Text.Encoding encoding);  
// Public Instance Properties ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C# 8.0 in a Nutshell

C# 8.0 in a Nutshell

Joseph Albahari, Eric Johannsen
C# 10 in a Nutshell

C# 10 in a Nutshell

Joseph Albahari
C# in a Nutshell, Second Edition

C# in a Nutshell, Second Edition

Peter Drayton, Ben Albahari, Ted Neward
Code like a Pro in C#

Code like a Pro in C#

Jort Rodenburg

Publisher Resources

ISBN: 0596001819Catalog PageErrata