The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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


Reads a specified maximum of characters from the current stream into a buffer, beginning at the specified index.

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

public override int Read(
	char[] buffer,
	int index,
	int count


Type: 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.

Type: System.Int32

The index of buffer at which to begin writing.

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.

Exception Condition

The buffer length minus index is less than count.


buffer is null.


index or count is negative.


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

This method overrides TextReader.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.

For a list of common I/O tasks, see Common I/O Tasks.

The following code example reads five characters at a time until the end of the file is reached.

using System;
using System.IO;

class Test 

    public static void Main() 
        string path = @"c:\temp\MyTest.txt";

            if (File.Exists(path)) 

            using (StreamWriter sw = new StreamWriter(path)) 
                sw.WriteLine("is some text");
                sw.WriteLine("to test");

            using (StreamReader sr = new StreamReader(path)) 
                //This is an arbitrary size for this example.
                char[] c = null;

                while (sr.Peek() >= 0) 
                    c = new char[5];
                    sr.Read(c, 0, c.Length);
                    //The output will look odd, because
                    //only five characters are read at a time.
        catch (Exception e) 
            Console.WriteLine("The process failed: {0}", e.ToString());

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
© 2015 Microsoft