ReadLine Method

StringReader.ReadLine Method ()


Reads a line of characters from the current string and returns the data as a string.

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

public override string ReadLine()

Return Value

Type: System.String

The next line from the current string, or null if the end of the string is reached.

Exception Condition

The current reader is closed.


There is insufficient memory to allocate a buffer for the returned string.

This method overrides the TextReader.ReadLine method.

A line is defined as a sequence of characters followed by a line feed ("\n"), a carriage return ("\r"), or a carriage return immediately followed by a line feed ("\r\n"). The string that is returned does not contain the terminating carriage return or line feed. The returned value is null if the end of the string has been reached.

If the current method throws an OutOfMemoryException, the reader's position in the underlying string is advanced by the number of characters the method was able to read, but the characters already read into the internal ReadLine buffer are discarded. Because the position of the reader in the string cannot be changed, the characters already read are unrecoverable, and can be accessed only by reinitializing the StringReader. To avoid such a situation, use the Read method and store the read characters in a preallocated buffer.

The following table lists examples of other typical or related I/O tasks.

To do this...

See the example in this topic...

Create a text file.

How to: Write Text to a File

Write to a text file.

How to: Write Text to a File

Read from a text file.

How to: Read Text from a File

Append text to a file.

How to: Open and Append to a Log File



Get the size of a file.


Get the attributes of a file.


Set the attributes of a file.


Determine if a file exists.


Read from a binary file.

How to: Read and Write to a Newly Created Data File

Write to a binary file.

How to: Read and Write to a Newly Created Data File

This code example is part of a larger example provided for the StringReader class.

// From textReaderText, create a continuous paragraph 
// with two spaces between each sentence.
string aLine, aParagraph = null;
StringReader strReader = new StringReader(textReaderText);
    aLine = strReader.ReadLine();
    if(aLine != null)
        aParagraph = aParagraph + aLine + " ";
        aParagraph = aParagraph + "\n";
Console.WriteLine("Modified text:\n\n{0}", aParagraph);

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