Share via


Read Method

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

Reads a block of bytes from the current stream and writes the data to a buffer.

Namespace:  System.IO
Assembly:  System.IO (in System.IO.dll)

Syntax

'Declaration
Public Overrides Function Read ( _
    buffer As Byte(), _
    offset As Integer, _
    count As Integer _
) As Integer
public override int Read(
    byte[] buffer,
    int offset,
    int count
)
public:
virtual int Read(
    array<unsigned char>^ buffer, 
    int offset, 
    int count
) override
abstract Read : 
        buffer:byte[] * 
        offset:int * 
        count:int -> int 
override Read : 
        buffer:byte[] * 
        offset:int * 
        count:int -> int 
public override function Read(
    buffer : byte[], 
    offset : int, 
    count : int
) : int

Parameters

  • buffer
    Type: array<System. . :: . .Byte> [] () [] []
    When this method returns, contains the specified byte array with the values between offset and (offset + count - 1) replaced by the characters read from the current stream.
  • offset
    Type: System. . :: . .Int32
    The zero-based byte offset in buffer at which to begin storing data from the current stream.

Return Value

Type: System. . :: . .Int32
The total number of bytes written into the buffer. This can be less than the number of bytes requested if that number of bytes are not currently available, or zero if the end of the stream is reached before any bytes are read.

Remarks

The offset parameter gives the offset of the first byte in buffer to which data from the current stream is written. The count parameter gives the maximum number of bytes to read from the current stream. The returned value is the actual number of bytes read, or zero if the end of the stream is reached.

If the read operation is successful, the current position within the stream advances by the number of bytes read. If an exception occurs, the current position within the stream remains unchanged.

The Read method will return zero only if the end of the stream is reached. In all other cases, Read always reads at least one byte from the stream before returning. By definition, if no data is available from the stream upon a call to Read, the Read method returns zero (the end of the stream is reached automatically). An implementation is free to return fewer bytes than requested even if the end of the stream has not been reached.

Use BinaryReader for reading primitive data types.

.NET Framework Security

See Also

Reference

MemoryStream Class

System.IO Namespace