Поделиться через


StreamReader.Read Method (array<Char[], Int32, Int32)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Updated: December 2010

When it is called by trusted applications, reads a maximum of count characters from the current stream into buffer, beginning at index.

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

Syntax

'Declaration
Public Overrides Function Read ( _
    <OutAttribute> buffer As Char(), _
    index As Integer, _
    count As Integer _
) As Integer
public override int Read(
    char[] buffer,
    int index,
    int count
)

Parameters

  • buffer
    Type: array<System.Char[]
    When this method returns, contains the specified character array with the values between index and (index + count - 1) replaced by the characters read from the current source.
  • index
    Type: System.Int32
    The index of buffer at which to begin writing.
  • count
    Type: System.Int32
    The maximum number of characters to read.

Return Value

Type: System.Int32
The number of characters that have been read, or 0 if at the end of the stream and no data was read. The number will be less than or equal to the count parameter, depending on whether the data is available within the stream.

Exceptions

Exception Condition
ArgumentException

The buffer length minus index is less than count.

ArgumentNullException

buffer is nulla null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

index or count is negative.

IOException

An I/O error occurs, such as the stream is closed.

Remarks

This method overrides Read.

This method returns an integer so that it can return 0 if the end of the stream has been reached.

When using the Read method, it is more efficient to use a buffer that is the same size as the internal buffer of the stream, where the internal buffer is set to your desired block size, and to always read less than the block size. If the size of the internal buffer was unspecified when the stream was constructed, its default size is 4 kilobytes (4096 bytes). If you manipulate the position of the underlying stream after reading data into the buffer, the position of the underlying stream might not match the position of the internal buffer. To reset the internal buffer, call the DiscardBufferedData method; however, this method slows performance and should be called only when absolutely necessary.

This method returns after either the number of characters specified by the count parameter are read, or the end of the file is reached. ReadBlock is a blocking version of Read.

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

 StreamReader.Read may throw IndexOutOfRangeException if the stream state becomes corrupted. On Silverlight for Windows Phone, the stream state may become corrupted if multiple threads read from or write to a stream object without using synchronization.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Change History

Date

History

Reason

December 2010

Added information about calling DiscardBufferedData.

Information enhancement.