C# BaseStream PropertyThis C# article demonstrates the BaseStream instance property. A BaseStream can be accessed on a BinaryReader.
is a property on a Stream. It returns information about the underlying buffer. BaseStream is available on all derived Stream instances. This property is useful in many programs.Stream
uses 2 System.IO objects. The MemoryStream stores the bytes in the specific file. And the BinaryReader type exposes the BaseStream property. The 2 using-statements enclose the BinaryReader object.Using
Tip: You can see that the BaseStream returns a reference to the MemoryStream instance that was passed to the BinaryReader.
C# program that uses BaseStream property
static void Main()
using (MemoryStream memory =
using (BinaryReader reader = new BinaryReader(memory))
// Get the BaseStream Stream.
Stream baseStream = reader.BaseStream;
// The BaseStream is the MemoryStream instance.
Console.WriteLine(baseStream.Length == memory.Length);
Console.WriteLine(baseStream is MemoryStream);
Type of BaseStream.
You can also see in the output of the program that the Stream returned by BaseStream is precisely equivalent to the MemoryStream. It is, however, referenced through its base class Stream.MemoryStream
The BaseStream property is defined on several types, including BinaryWriter, BinaryReader, StreamWriter and StreamReader. The best reason to use BaseStream is if your program's design doesn't allow you to directly access the base stream.BinaryWriterBinaryReaderStreamWriterStreamReader
If you design a method that receives a BinaryReader parameter directly, you can call BaseStream to get the underlying Stream reference. In the example shown above, we could easily access the MemoryStream. This is not always possible.
Summary. BaseStream is useful when using classes that act upon underlying Streams. Often we need a reference to the original Stream to get its total Length or type. This helps when developing methods that receive types with underlying Streams.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to email@example.com.