StringReader.ReadLine Method ()
Reads a line of characters from the current string and returns the data as a string.
Assembly: mscorlib (in mscorlib.dll)
Return Value
Type: System.StringThe next line from the current string, or null if the end of the string is reached.
| Exception | Condition |
|---|---|
| ObjectDisposedException | The current reader is closed. |
| OutOfMemoryException | 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. | |
Write to a text file. | |
Read from a text file. | |
Append text to a 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. | |
Write to a binary 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); while(true) { aLine = strReader.ReadLine(); if(aLine != null) { aParagraph = aParagraph + aLine + " "; } else { aParagraph = aParagraph + "\n"; break; } } Console.WriteLine("Modified text:\n\n{0}", aParagraph);
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1