System.IO.TextReader Class

public abstract class TextReader : MarshalByRefObject, IDisposable

Base Types

Object
  MarshalByRefObject
    TextReader

This type implements IDisposable.

Assembly

mscorlib

Library

BCL

Summary

Represents an object that can read a sequential series of characters.

Description

TextReader is designed for character input, whereas the StreamReader is designed for byte input and the StringReader class is designed for reading from a string.

By default, a TextReader is not thread safe. For information on creating a thread-safe TextReader , see System.IO.TextReader.Synchronized(System.IO.TextReader) .

See Also

System.IO Namespace

Members

TextReader Constructors

TextReader Constructor

TextReader Methods

TextReader.Close Method
TextReader.Dispose Method
TextReader.Peek Method
TextReader.Read(char[], int, int) Method
TextReader.Read() Method
TextReader.ReadBlock Method
TextReader.ReadLine Method
TextReader.ReadToEnd Method
TextReader.Synchronized Method
TextReader.System.IDisposable.Dispose Method

TextReader Fields

TextReader.Null Field


TextReader Constructor

protected TextReader();

Summary

Constructs a new instance of the TextReader class.

See Also

System.IO.TextReader Class, System.IO Namespace

TextReader.Close Method

public virtual void Close();

Summary

Closes the current TextReader instance and releases any system resources associated with it.

Description

[Note: After a call to System.IO.TextReader.Close , any IO operation on the current instance might throw an exception.

]

[Behaviors: This method is equivalent to System.IO.TextReader.Dispose(System.Boolean)( true ).]

[Usage: Use this method to close the current instance and free any resources associated with it.]

See Also

System.IO.TextReader Class, System.IO Namespace

TextReader.Dispose Method

protected virtual void Dispose(bool disposing);

Summary

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

Parameters

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

Description

When the disposing parameter is true , this method releases all resources held by any managed objects that this TextReader references. This method invokes the Dispose() method of each referenced object.

[Note: System.IO.TextReader.Dispose(System.Boolean) can be called multiple times by other objects. When overriding System.IO.TextReader.Dispose(System.Boolean)(Boolean), be careful not to reference objects that have been previously disposed in an earlier call to System.IO.TextReader.Dispose(System.Boolean) .]

See Also

System.IO.TextReader Class, System.IO Namespace

TextReader.Peek Method

public virtual int Peek();

Summary

Reads the next character without changing the state of the reader or the character source.

Return Value

The next character to be read, or -1 if no more characters are available.

Exceptions

Exception TypeCondition
IOExceptionAn I/O error has occurred.

Description

The position of the TextReader in the source is not changed by this operation.

[Behaviors: As described above.]

[Default: The default implementation returns -1.]

See Also

System.IO.TextReader Class, System.IO Namespace

TextReader.Read(char[], int, int) Method

public virtual int Read(char[] buffer, int index, int count);

Summary

Reads at most the specified number of characters from the current character source, and writes them to the provided character array.

Parameters

buffer
A Char array. 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
A Int32 that specifies the place in buffer at which to begin writing.
count
A Int32 that specifies the maximum number of characters to read. If the end of the stream is reached before count of characters is read into buffer , this method returns.

Return Value

A Int32 containing the number of characters that were read, or zero if there were no more characters left to read. Can be less than count if the end of the stream has been reached.

Exceptions

Exception TypeCondition
ArgumentNullExceptionbuffer is null .
ArgumentException(index + count ) > buffer. Length.
ArgumentOutOfRangeExceptionindex < 0

- or-

count < 0.

IOExceptionAn I/O error occurred.

Description

System.IO.TextReader.ReadBlock(System.Char[],System.Int32,System.Int32) is a blocking version of this method.

[Behaviors: The provided character array can be changed only in the specified range.]

See Also

System.IO.TextReader Class, System.IO Namespace

TextReader.Read() Method

public virtual int Read();

Summary

Reads the next character from the character source and advances the character position by one character.

Return Value

The next character from the character source represented as a Int32 , or -1 if at the end of the stream.

Exceptions

Exception TypeCondition
IOException An I/O error occurred.

Description

[Behaviors: As described above.]

[Default: The default implementation returns -1.]

See Also

System.IO.TextReader Class, System.IO Namespace

TextReader.ReadBlock Method

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

Summary

Reads a specified number of characters from the current stream into a provided character array.

Parameters

buffer
A Char array. 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
A Int32 that specifies the index in buffer at which to begin writing.
count
A Int32 that specifies the maximum number of characters to read.

Return Value

A Int32 containing the number of characters that were read, or zero if there were no more characters left to read. Can be less than count if the end of the stream has been reached.

Exceptions

Exception TypeCondition
ArgumentNullExceptionbuffer is null .
ArgumentException(index + count - 1) > buffer. Length.
ArgumentOutOfRangeExceptionindex < 0

- or-

count < 0.

IOException An I/O error occurred.

Description

The method blocks until either the specified number of characters are read, or no more characters are available in the source.

[Behaviors: As described above.]

See Also

System.IO.TextReader Class, System.IO Namespace

TextReader.ReadLine Method

public virtual string ReadLine();

Summary

Reads a line of characters from the current character source.

Return Value

A String containing the next line from the input stream, or null if all lines have been read. The returned string does not contain the line terminating character.

Exceptions

Exception TypeCondition
IOException An I/O error occurred.
OutOfMemoryExceptionThere is insufficient memory to allocate a buffer for the returned string.

ArgumentOutOfRangeExceptionThe number of characters in the next line is larger than System.Int32.MaxValue.

Description

A line is defined as a sequence of characters followed by a carriage return (0x000d), a line feed (0x000a), System.Environment.NewLine, or the end of stream marker.

[Behaviors: As described above.]

See Also

System.IO.TextReader Class, System.IO Namespace

TextReader.ReadToEnd Method

public virtual string ReadToEnd();

Summary

Reads all characters from the current position in the character source to the end of the source.

Return Value

A string containing all characters from the current position to the end of the character source.

Exceptions

Exception TypeCondition
IOException An I/O error occurred.
OutOfMemoryExceptionThere is insufficient memory to allocate a buffer for the returned string.
ArgumentOutOfRangeExceptionThe number of characters from the current position to the end of the underlying stream is larger than System.Int32.MaxValue.

Description

[Behaviors: As described above.]

See Also

System.IO.TextReader Class, System.IO Namespace

TextReader.Synchronized Method

public static TextReader Synchronized(TextReader reader);

Summary

Creates a thread-safe wrapper around the specified TextReader instance.

Parameters

reader
The TextReader to synchronize.

Return Value

A thread-safe TextReader .

Exceptions

Exception TypeCondition
ArgumentNullExceptionThe reader parameter is null .

Description

This method returns a TextReader instance that wraps around the specified TextReader instance and restricts concurrent access to it by multiple threads.

See Also

System.IO.TextReader Class, System.IO Namespace

TextReader.System.IDisposable.Dispose Method

void IDisposable.Dispose();

Summary

Implemented to support the IDisposable interface. [Note: For more information, see System.IDisposable.Dispose.]

See Also

System.IO.TextReader Class, System.IO Namespace

TextReader.Null Field

public static readonly TextReader Null;

Summary

Provides a TextReader with no data to read from.

Description

Reading from the System.IO.TextReader.Null text reader is similar to reading from the end of a stream:

See Also

System.IO.TextReader Class, System.IO Namespace