TextReader Class

Definition

Represents a reader that can read a sequential series of characters.

[Serializable]
[ComVisible(true)]
public abstract class TextReader : IDisposable
Inheritance
TextReader
Derived

Inherited Members

System.Object

Constructors

TextReader()

Initializes a new instance of the TextReader class.

protected TextReader()

Fields

Null

Provides a TextReader with no data to read from.

public static readonly TextReader Null

Methods

Dispose()

Releases all resources used by the TextReader object.

public void Dispose()

Dispose(Boolean)

Releases the unmanaged resources used by the TextReader and optionally releases the managed resources.

protected virtual void Dispose(bool disposing)
Parameters
disposing
Boolean

true to release both managed and unmanaged resources; false to release only unmanaged resources.

Peek()

Reads the next character without changing the state of the reader or the character source. Returns the next available character without actually reading it from the reader.

public virtual int Peek()
Returns

An integer representing the next character to be read, or -1 if no more characters are available or the reader does not support seeking.

Exceptions

An I/O error occurs.

Read()

Reads the next character from the text reader and advances the character position by one character.

public virtual int Read()
Returns

The next character from the text reader, or -1 if no more characters are available. The default implementation returns -1.

Exceptions

An I/O error occurs.

Read(Char[], Int32, Int32)

Reads a specified maximum number of characters from the current reader and writes the data to a buffer, beginning at the specified index.

public virtual int Read(char[] buffer, int index, int count)
Parameters
buffer
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
Int32

The position in buffer at which to begin writing.

count
Int32

The maximum number of characters to read. If the end of the reader is reached before the specified number of characters is read into the buffer, the method returns.

Returns

The number of characters that have been read. The number will be less than or equal to count, depending on whether the data is available within the reader. This method returns 0 (zero) if it is called when no more characters are left to read.

Exceptions

buffer is null.

The buffer length minus index is less than count.

index or count is negative.

An I/O error occurs.

ReadAsync(Char[], Int32, Int32)

Reads a specified maximum number of characters from the current text reader asynchronously and writes the data to a buffer, beginning at the specified index.

[HostProtection(ExternalThreading = true)]
[ComVisible(false)]
public virtual Task<int> ReadAsync(char[] buffer, int index, int count)
Parameters
buffer
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
Int32

The position in buffer at which to begin writing.

count
Int32

The maximum number of characters to read. If the end of the text is reached before the specified number of characters is read into the buffer, the current method returns.

Returns

A task that represents the asynchronous read operation. The value of the TResult parameter contains the total number of bytes read into the buffer. The result value can be less than the number of bytes requested if the number of bytes currently available is less than the requested number, or it can be 0 (zero) if the end of the text has been reached.

Exceptions

buffer is null.

index or count is negative.

The sum of index and count is larger than the buffer length.

The text reader has been disposed.

The reader is currently in use by a previous read operation.

ReadBlock(Char[], Int32, Int32)

Reads a specified maximum number of characters from the current text reader and writes the data to a buffer, beginning at the specified index.

public virtual int ReadBlock(char[] buffer, int index, int count)
Parameters
buffer
Char[]

When this method returns, this parameter contains the specified character array with the values between index and (index + count -1) replaced by the characters read from the current source.

index
Int32

The position in buffer at which to begin writing.

count
Int32

The maximum number of characters to read.

Returns

The number of characters that have been read. The number will be less than or equal to count, depending on whether all input characters have been read.

Exceptions

buffer is null.

The buffer length minus index is less than count.

index or count is negative.

An I/O error occurs.

ReadBlockAsync(Char[], Int32, Int32)

Reads a specified maximum number of characters from the current text reader asynchronously and writes the data to a buffer, beginning at the specified index.

[HostProtection(ExternalThreading = true)]
[ComVisible(false)]
public virtual Task<int> ReadBlockAsync(char[] buffer, int index, int count)
Parameters
buffer
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
Int32

The position in buffer at which to begin writing.

count
Int32

The maximum number of characters to read. If the end of the text is reached before the specified number of characters is read into the buffer, the current method returns.

Returns

A task that represents the asynchronous read operation. The value of the TResult parameter contains the total number of bytes read into the buffer. The result value can be less than the number of bytes requested if the number of bytes currently available is less than the requested number, or it can be 0 (zero) if the end of the text has been reached.

Exceptions

buffer is null.

index or count is negative.

The sum of index and count is larger than the buffer length.

The text reader has been disposed.

The reader is currently in use by a previous read operation.

ReadLine()

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

public virtual string ReadLine()
Returns

The next line from the reader, or null if all characters have been read.

Exceptions

An I/O error occurs.

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

The number of characters in the next line is larger than MaxValue

ReadLineAsync()

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

[HostProtection(ExternalThreading = true)]
[ComVisible(false)]
public virtual Task<string> ReadLineAsync()
Returns

A task that represents the asynchronous read operation. The value of the TResult parameter contains the next line from the text reader, or is null if all of the characters have been read.

Exceptions

The number of characters in the next line is larger than MaxValue.

The text reader has been disposed.

The reader is currently in use by a previous read operation.

ReadToEnd()

Reads all characters from the current position to the end of the text reader and returns them as one string.

public virtual string ReadToEnd()
Returns

A string that contains all characters from the current position to the end of the text reader.

Exceptions

An I/O error occurs.

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

The number of characters in the next line is larger than MaxValue

ReadToEndAsync()

Reads all characters from the current position to the end of the text reader asynchronously and returns them as one string.

[HostProtection(ExternalThreading = true)]
[ComVisible(false)]
public virtual Task<string> ReadToEndAsync()
Returns

A task that represents the asynchronous read operation. The value of the TResult parameter contains a string with the characters from the current position to the end of the text reader.

Exceptions

The number of characters is larger than MaxValue.

The text reader has been disposed.

The reader is currently in use by a previous read operation.