Stream Class
Provides an abstract base class for all streams in the .NET Framework. This is an abstract class.
Usage
The Stream class is the base class for all stream types in .NET. It provides a generic view of a sequence of bytes, such as data from a file, an input/output device, or an inter-process communication pipe.
Key operations on streams include:
- Reading data from the stream.
- Writing data to the stream.
- Seeking to a specific position within the stream.
- Checking if the stream supports specific operations (e.g., CanRead, CanWrite, CanSeek).
- Flushing any buffered data.
Common derived classes include:
FileStreamfor file I/O.MemoryStreamfor in-memory operations.NetworkStreamfor network communication.BufferedStreamfor improved performance by buffering.
Example
This example demonstrates reading from a MemoryStream:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Extensions.Logging.Abstractions" version="5.0.0" targetFramework="net5.0" />
</packages>
using System;
using System.IO;
public class Example
{
public static void Main()
{
// Create a byte array.
byte[] data = { 0x01, 0x02, 0x03, 0x04, 0x05 };
// Create a MemoryStream from the byte array.
using (MemoryStream ms = new MemoryStream(data))
{
// Read byte by byte.
int byteRead;
while ((byteRead = ms.ReadByte()) != -1)
{
Console.Write($"{byteRead:X2} ");
}
// Expected output: 01 02 03 04 05
}
}
}
Syntax
: MarshalByRefObject
Constructors
The Stream class has a protected constructor that is intended to be called by derived class constructors. It does not have public constructors.
Fields
The Stream class defines the following protected fields:
protected byte[] InternalBuffer;protected int BufferSize;
Properties
CanRead- Gets a value indicating whether the current stream supports reading.
CanSeek- Gets a value indicating whether the current stream supports seeking and seeking is possible.
CanTimeout- Gets a value indicating whether the current stream supports setting the timeout.
CanWrite- Gets a value indicating whether the current stream supports writing.
Length- Gets the length in bytes of the stream.
Position- Gets or sets the current position within the stream.
Methods
Close()- Releases the unmanaged resources that are used by the current instance of the
Streamclass and optionally releases the managed resources. CopyTo(Stream destination)- Copies the current stream to a destination.
Dispose()- Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
Flush()- Clears buffers for this stream and causes any buffered data to be written to the underlying device.
Read(byte[] buffer, int offset, int count)- When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)- Asynchronously reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
ReadByte()- Reads one byte from the stream and advances the position in the stream by one byte, or returns -1 if at the end of the stream.
Seek(long offset, SeekOrigin origin)- Sets the position within the current stream to the specified value.
SetLength(long value)- Sets the length of the current stream.
Write(byte[] buffer, int offset, int count)- When overridden in a derived class, writes a sequence of bytes to the current stream and advances the position within the stream by the number of bytes written.
WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)- Asynchronously writes a sequence of bytes to the current stream and advances the position within the stream by the number of bytes written.
WriteByte(byte value)- Writes one byte to the current stream and advances the position within the stream by one byte.
Inheritance Hierarchy
Object
MarshalByRefObject
Stream
The Stream class is the abstract base class for the following derived classes:
System.IO.BufferedStreamSystem.IO.FileStreamSystem.IO.IsolatedStorage.IsolatedStorageFileStreamSystem.IO.MemoryStreamSystem.Net.Sockets.NetworkStreamSystem.IO.Pipes.PipeStreamSystem.Security.Cryptography.CryptoStreamSystem.Xml.XmlDictionaryReader.XmlDictionaryStreamSystem.Xml.XmlReader.XmlReaderStream
Implements
The Stream class implements the following interfaces:
System.IDisposable