C# Stream IO (Input/Output)

The Stream abstract base class supports reading and writing bytes. All classes representing streams inherit from the Stream class. The Stream class and its derived classes provide a common view of data sources and files and isolate the programmer from the specifics of the operating system and underlying devices.

Stream comes under the System.IO stream is an abstract class which allow us read write and transfer byte into the source. Those classes which are using for read or write or transfer byte to a specific source must use the System.IO.Stream class.

Below three basic operations Stream:

Read - Transfers data from a stream to a data structure, as a series of bytes.

Write - transfer data to a stream from a data source.

Find - request and modify the current position in a series

commonly used stream classes:

  • FileStream: To read and write to a file.
  • IsolatedStorageFileStream: To read and write to a file in isolated storage.
  • MemoryStream: Read and write memory as backup storage.
  • BufferedStream: To improve the performance of reading and write operations.
  • NetworkStream: Read and write via network connectors.
  • PipeStream: To read and write named and anonymous pipes.
  • CryptoStream: to link data streams to cryptographic transformations.

Typically, streams are designed for input and output bytes. The reader and author type converts encoded characters to bytes for the stream to complete the action. Each reader and author class has a corresponding stream that can be retrieved with the class' BaseStream property.

Stream Readers and Writers

Here are some common classes from readers and writers:

  • BinaryReader and BinaryWriter: To read and write primitive data types as binary values.
  • StreamReader and StreamWriter: Reads and writes characters using an encoding value to convert characters to bytes.
  • StringReader and StringWriter: to read and write characters to and from strings.
  • TextReader and TextWriter: act as abstract base classes for other readers and writers who read and write characters and strings, but not binary data.