Stream Class
Represents a base class for all streams. A stream is an abstraction of a sequence of bytes, such as a file, an input socket, or a pipe. The system uses streams to read from and write to various data sources and sinks.
Inheritance: Object > Stream
Derived classes: MemoryStream, FileStream, BufferedStream, etc.
Public Constructors
The Stream class has no public constructors. It is an abstract class.
Public Properties
-
CanRead bool CanRead { get; }
Gets a value indicating whether the stream supports reading.
-
CanSeek bool CanSeek { get; }
Gets a value indicating whether the stream supports seeking.
-
CanWrite bool CanWrite { get; }
Gets a value indicating whether the stream supports writing.
-
Length long Length { get; }
Gets the length in bytes of the stream.
-
Position long Position { get; set; }
Gets or sets the current position within the stream.
Public Methods
-
BeginRead IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
Asynchronously reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
-
BeginWrite IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
Asynchronously writes a sequence of bytes to the current stream and advances the position within the stream by the number of bytes written.
-
Close void Close()
Releases the unmanaged resources that are used by the current instance of the Stream class and optionally releases the managed resources.
-
CopyTo void CopyTo(Stream destination)
Copies all bytes from the current stream to another stream.
-
EndRead int EndRead(IAsyncResult asyncResult)
Begins an asynchronous read operation.
-
EndWrite void EndWrite(IAsyncResult asyncResult)
Ends a pending asynchronous write operation.
-
Flush void Flush()
Clears all buffers for this stream and causes any buffered data to be written to the underlying device.
-
Read int 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.
-
Seek long Seek(long offset, SeekOrigin origin)
When overridden in a derived class, sets the position within the current stream.
-
SetLength void SetLength(long value)
When overridden in a derived class, sets the length of the current stream.
-
Write void 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.
Protected Methods
-
Dispose void Dispose(bool disposing)
Releases the unmanaged resources that are used by the current instance of the Stream class and optionally releases the managed resources.
Example Usage
Here's a simple example demonstrating how to write to and read from a MemoryStream:
using System;
using System.IO;
using System.Text;
public class Example
{
public static void Main()
{
// Create a MemoryStream
using (MemoryStream ms = new MemoryStream())
{
// Write some bytes to the stream
byte[] dataToWrite = Encoding.UTF8.GetBytes("Hello, Stream!");
ms.Write(dataToWrite, 0, dataToWrite.Length);
// Ensure all data is written
ms.Flush();
// Reset the stream position to the beginning to read
ms.Position = 0;
// Read bytes from the stream
byte[] buffer = new byte[ms.Length];
int bytesRead = ms.Read(buffer, 0, buffer.Length);
// Convert the bytes back to a string
string content = Encoding.UTF8.GetString(buffer, 0, bytesRead);
Console.WriteLine($"Content read from stream: {content}");
}
}
}