System.IO.Stream

Overview

The System.IO.Stream abstract class provides a generic view of a sequence of bytes, such as a file, an I/O device, an inter-process communication pipe, or a TCP/IP socket.

All stream classes inherit from this type, enabling consistent operations for reading, writing, and seeking.

Inheritance

ObjectMarshalByRefObjectSystem.IO.Stream

Methods ▼

SignatureDescription
int Read(byte[] buffer, int offset, int count)Reads a block of bytes from the stream and writes the data to a buffer.
Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)Asynchronously reads a block of bytes from the stream.
void Write(byte[] buffer, int offset, int count)Writes a block of bytes to the stream.
Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)Asynchronously writes a block of bytes to the stream.
long Seek(long offset, SeekOrigin origin)Sets the position within the current stream.
void Flush()Clears all buffers for the current stream and causes any buffered data to be written.
Task FlushAsync(CancellationToken cancellationToken)Asynchronously clears all buffers for the current stream.
void Close()Closes the current stream and releases any resources.

Properties ▼

PropertyDescription
bool CanReadGets a value indicating whether the current stream supports reading.
bool CanWriteGets a value indicating whether the current stream supports writing.
bool CanSeekGets a value indicating whether the current stream supports seeking.
long LengthGets the length of the stream in bytes.
long PositionGets or sets the current position within the stream.

Events ▼

The Stream class does not expose any events directly. Derived classes may provide events such as Disposed.

Remarks

When creating a custom stream, you must override the abstract members Read, Write, Seek, SetLength, and the properties CanRead, CanWrite, CanSeek, Length, and Position. Implementations should also override Dispose(bool) to release unmanaged resources.

Example

using System; using System.IO; using System.Text; class UpperCaseStream : Stream { private readonly Stream _baseStream; public UpperCaseStream(Stream baseStream) => _baseStream = baseStream; public override bool CanRead => _baseStream.CanRead; public override bool CanSeek => _baseStream.CanSeek; public override bool CanWrite => _baseStream.CanWrite; public override long Length => _baseStream.Length; public override long Position { get => _baseStream.Position; set => _baseStream.Position = value; } public override void Flush() => _baseStream.Flush(); public override int Read(byte[] buffer, int offset, int count) { int bytesRead = _baseStream.Read(buffer, offset, count); for (int i = offset; i < offset + bytesRead; i++) buffer[i] = (byte)char.ToUpper((char)buffer[i]); return bytesRead; } public override void Write(byte[] buffer, int offset, int count) { byte[] upper = new byte[count]; for (int i = 0; i < count; i++) upper[i] = (byte)char.ToUpper((char)buffer[offset + i]); _baseStream.Write(upper, 0, count); } public override long Seek(long offset, SeekOrigin origin) => _baseStream.Seek(offset, origin); public override void SetLength(long value) => _baseStream.SetLength(value); } class Program { static void Main() { using var fs = new FileStream("example.txt", FileMode.Create, FileAccess.Write); using var ucs = new UpperCaseStream(fs); byte[] data = Encoding.UTF8.GetBytes("Hello, World!"); ucs.Write(data, 0, data.Length); } }