03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

416 ❘ CHAPTER 18 Streams<br />

The code first creates a MemoryStream. It then creates a BinaryWriter associated with the stream<br />

and uses its Write method to write a string into it.<br />

Next, the code uses the Seek method to rewind the stream to the beginning of the data. It then creates<br />

a BinaryReader associated with the stream, uses its ReadString method to read a string from the<br />

stream, and displays the string in a message box.<br />

The code finishes by disposing of the objects it used. This is a bit more confusing than usual<br />

because the reader and writer are associated with the stream. When the program disposes of the<br />

reader or writer, those objects automatically close their underlying stream. That means you cannot<br />

dispose of the writer before you finish with the reader. If you are careful, you can use a properly<br />

ordered sequence of using statements, but this example seems simpler if you just dispose of the<br />

objects all at once at the end.<br />

BinaryReader and BinaryWriter<br />

The BinaryReader and BinaryWriter classes are helper classes that work with stream classes.<br />

They provide an interface that makes it easier to read and write data in a stream. For example, the<br />

BinaryReader class’s ReadInt32 method reads a 4-byte (32-bit) signed integer from the stream.<br />

Similarly, the ReadUInt16 method reads a 2-byte (16-bit) unsigned integer.<br />

These classes still work at a relatively low level, and you should generally use higher-level classes to<br />

read and write data if possible. For example, you shouldn’t tie yourself to a particular representation<br />

of an integer (32- or 16-bit) unless you must.<br />

Both the BinaryReader and BinaryWriter classes have a BaseStream property that returns a<br />

reference to the underlying stream. Note that their Close and Dispose methods automatically<br />

close their underlying streams.<br />

The following table describes the BinaryReader class’s most useful methods.<br />

Method<br />

Close<br />

PeekChar<br />

Read<br />

ReadBoolean<br />

ReadByte<br />

Purpose<br />

Closes the BinaryReader and its underlying stream.<br />

Reads the stream’s next character but does not advance<br />

the reader’s position.<br />

Reads characters from the stream and advances the reader’s<br />

position.<br />

Reads a bool from the stream and advances the reader’s<br />

position by 1 byte.<br />

Reads a byte from the stream and advances the reader’s<br />

position by 1 byte.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!