The Stream Classes

The .NET Framework defines both byte and character stream classes. However, the character stream classes are really just wrappers that convert an underlying byte stream to a character stream, handling any conversion automatically. Thus, the character streams, although logically separate, are built upon byte streams.
The core stream classes are defined within the System.IO namespace. To use these classes, you will usually include the following statement near the top of your program:
using System.IO;
The reason that you don’t have to specify System.IO for console input and output is that the Console class is defined in the System namespace.

The Stream Class

The core stream class is System.IO.Stream. Stream represents a byte stream and is a base class for all other stream classes. It is also abstract, which means that you cannot instantiate a Stream object. Stream defines a set of standard stream operations. Following Table shows several commonly used methods defined by Stream.
Several of the methods shown in Following Table will throw an IOException if an I/O error occurs. If an invalid operation is attempted, such as attempting to write to a stream that is read-only, a NotSupportedException is thrown. Other exceptions are possible, depending on the specific method.

Method Description
void Close( ) Closes the stream.
void Flush( ) Writes the contents of the stream to the physical device.
int ReadByte( ) Returns an integer representation of the next available byte of input. Returns –1 when the end of the file is encountered.
int Read(byte[ ] buffer, int offset, int count) Attempts to read up to count bytes into buffer starting at buffer[offset], returning the number of bytes successfully read.
long Seek(long offset, SeekOrigin origin) Sets the current position in the stream to the specified offset from the specified origin. It returns the new position.
void WriteByte(byte value) Writes a single byte to an output stream.
int Write(byte[ ] buffer, int offset, int count) Writes a subrange of count bytes from the array buffer, beginning at buffer[offset], returning the number of bytes written.

Notice that Stream defines methods that read and write data. However, not all streams will support both of these operations, because it is possible to open read-only or write-only streams. Also, not all streams will support position requests via Seek( ). To determine the capabilities of a stream, you will use one or more of Stream’s properties.