BinaryReader and BinaryWriter

To go beyond raw byte sequences when dealing with I/O data, the BCL designers invented the BinaryReader and BinaryWriter classes. Just as StreamReader and StreamWriter can be constructed by passing in a Stream object to a constructor, this abstraction simplifies the use of streams by providing additional methods. For example, where StreamReader concerns itself with the notion of textual data and provides a ReadLine method, the BinaryReader class exposes Read methods for various primitive multibyte data types.

Let’s look at an example, again using binary files:

using (var fs = File.OpenRead(@"c:\windows\system32\notepad.exe"))using (var br = new BinaryReader(fs)){    br.BaseStream.Position ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.