TextWriter Class

Definition

Represents a writer that can write a sequential series of characters. This class is abstract.

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

Inherited Members

System.Object

Constructors

TextWriter()

Initializes a new instance of the TextWriter class.

protected TextWriter()

TextWriter(IFormatProvider)

Initializes a new instance of the TextWriter class with the specified format provider.

protected TextWriter(IFormatProvider formatProvider)
Parameters
formatProvider
IFormatProvider

An IFormatProvider object that controls formatting.

Fields

CoreNewLine

Stores the newline characters used for this TextWriter.

protected char[] CoreNewLine

Null

Provides a TextWriter with no backing store that can be written to, but not read from.

public static readonly TextWriter Null

Properties

Encoding

When overridden in a derived class, returns the character encoding in which the output is written.

public abstract Encoding Encoding { get; }
Value
Encoding

The character encoding in which the output is written.

FormatProvider

Gets an object that controls formatting.

public virtual IFormatProvider FormatProvider { get; }
Value
IFormatProvider

An IFormatProvider object for a specific culture, or the formatting of the current culture if no other culture is specified.

NewLine

Gets or sets the line terminator string used by the current TextWriter.

public virtual string NewLine { get; set; }
Value
String

The line terminator string for the current TextWriter.

Methods

Dispose()

Releases all resources used by the TextWriter object.

public void Dispose()

Dispose(Boolean)

Releases the unmanaged resources used by the TextWriter 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.

Flush()

Clears all buffers for the current writer and causes any buffered data to be written to the underlying device.

public virtual void Flush()

FlushAsync()

Asynchronously clears all buffers for the current writer and causes any buffered data to be written to the underlying device.

[HostProtection(ExternalThreading = true)]
[ComVisible(false)]
public virtual Task FlushAsync()
Returns

A task that represents the asynchronous flush operation.

Exceptions

The text writer is disposed.

The writer is currently in use by a previous write operation.

Write(Boolean)

Writes the text representation of a Boolean value to the text string or stream.

public virtual void Write(bool value)
Parameters
value
Boolean

The Boolean value to write.

Exceptions

An I/O error occurs.

Write(Char)

Writes a character to the text string or stream.

public virtual void Write(char value)
Parameters
value
Char

The character to write to the text stream.

Exceptions

An I/O error occurs.

Write(Char[])

Writes a character array to the text string or stream.

public virtual void Write(char[] buffer)
Parameters
buffer
Char[]

The character array to write to the text stream.

Exceptions

An I/O error occurs.

Write(Char[], Int32, Int32)

Writes a subarray of characters to the text string or stream.

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

The character array to write data from.

index
Int32

The character position in the buffer at which to start retrieving data.

count
Int32

The number of characters to write.

Exceptions

The buffer length minus index is less than count.

The buffer parameter is null.

index or count is negative.

An I/O error occurs.

Write(Decimal)

Writes the text representation of a decimal value to the text string or stream.

public virtual void Write(decimal value)
Parameters
value
Decimal

The decimal value to write.

Exceptions

An I/O error occurs.

See Also

Write(Double)

Writes the text representation of an 8-byte floating-point value to the text string or stream.

public virtual void Write(double value)
Parameters
value
Double

The 8-byte floating-point value to write.

Exceptions

An I/O error occurs.

See Also

Write(Int32)

Writes the text representation of a 4-byte signed integer to the text string or stream.

public virtual void Write(int value)
Parameters
value
Int32

The 4-byte signed integer to write.

Exceptions

An I/O error occurs.

See Also

Write(Int64)

Writes the text representation of an 8-byte signed integer to the text string or stream.

public virtual void Write(long value)
Parameters
value
Int64

The 8-byte signed integer to write.

Exceptions

An I/O error occurs.

See Also

Write(Object)

Writes the text representation of an object to the text string or stream by calling the ToString method on that object.

public virtual void Write(object value)
Parameters
value
Object

The object to write.

Exceptions

An I/O error occurs.

See Also

Write(Single)

Writes the text representation of a 4-byte floating-point value to the text string or stream.

public virtual void Write(float value)
Parameters
value
Single

The 4-byte floating-point value to write.

Exceptions

An I/O error occurs.

See Also

Write(String)

Writes a string to the text string or stream.

public virtual void Write(string value)
Parameters
value
String

The string to write.

Exceptions

An I/O error occurs.

Write(String, Object)

Writes a formatted string to the text string or stream, using the same semantics as the Format(String, Object) method.

public virtual void Write(string format, object arg0)
Parameters
format
String

A composite format string (see Remarks).

arg0
Object

The object to format and write.

Exceptions

format is null.

An I/O error occurs.

format is not a valid composite format string.

-or-

The index of a format item is less than 0 (zero), or greater than or equal to the number of objects to be formatted (which, for this method overload, is one).

See Also

Write(String, Object, Object)

Writes a formatted string to the text string or stream, using the same semantics as the Format(String, Object, Object) method.

public virtual void Write(string format, object arg0, object arg1)
Parameters
format
String

A composite format string (see Remarks).

arg0
Object

The first object to format and write.

arg1
Object

The second object to format and write.

Exceptions

format is null.

An I/O error occurs.

format is not a valid composite format string.

-or-

The index of a format item is less than 0 (zero) or greater than or equal to the number of objects to be formatted (which, for this method overload, is two).

See Also

Write(String, Object, Object, Object)

Writes a formatted string to the text string or stream, using the same semantics as the Format(String, Object, Object, Object) method.

public virtual void Write(string format, object arg0, object arg1, object arg2)
Parameters
format
String

A composite format string (see Remarks).

arg0
Object

The first object to format and write.

arg1
Object

The second object to format and write.

arg2
Object

The third object to format and write.

Exceptions

format is null.

An I/O error occurs.

format is not a valid composite format string.

-or-

The index of a format item is less than 0 (zero), or greater than or equal to the number of objects to be formatted (which, for this method overload, is three).

See Also

Write(String, Object[])

Writes a formatted string to the text string or stream, using the same semantics as the Format(String, Object[]) method.

public virtual void Write(string format, params object[] arg)
Parameters
format
String

A composite format string (see Remarks).

arg
Object[]

An object array that contains zero or more objects to format and write.

Exceptions

format or arg is null.

An I/O error occurs.

format is not a valid composite format string.

-or-

The index of a format item is less than 0 (zero), or greater than or equal to the length of the arg array.

See Also

Write(UInt32)

Writes the text representation of a 4-byte unsigned integer to the text string or stream.

[CLSCompliant(false)]
public virtual void Write(uint value)
Parameters
value
UInt32

The 4-byte unsigned integer to write.

Attributes
Exceptions

An I/O error occurs.

See Also

Write(UInt64)

Writes the text representation of an 8-byte unsigned integer to the text string or stream.

[CLSCompliant(false)]
public virtual void Write(ulong value)
Parameters
value
UInt64

The 8-byte unsigned integer to write.

Attributes
Exceptions

An I/O error occurs.

See Also

WriteAsync(Char)

Writes a character to the text string or stream asynchronously.

[HostProtection(ExternalThreading = true)]
[ComVisible(false)]
public virtual Task WriteAsync(char value)
Parameters
value
Char

The character to write to the text stream.

Returns

A task that represents the asynchronous write operation.

Exceptions

The text writer is disposed.

The text writer is currently in use by a previous write operation.

WriteAsync(Char[])

Writes a character array to the text string or stream asynchronously.

[HostProtection(ExternalThreading = true)]
[ComVisible(false)]
public Task WriteAsync(char[] buffer)
Parameters
buffer
Char[]

The character array to write to the text stream. If buffer is null, nothing is written.

Returns

A task that represents the asynchronous write operation.

Exceptions

The text writer is disposed.

The text writer is currently in use by a previous write operation.

WriteAsync(Char[], Int32, Int32)

Writes a subarray of characters to the text string or stream asynchronously.

[HostProtection(ExternalThreading = true)]
[ComVisible(false)]
public virtual Task WriteAsync(char[] buffer, int index, int count)
Parameters
buffer
Char[]

The character array to write data from.

index
Int32

The character position in the buffer at which to start retrieving data.

count
Int32

The number of characters to write.

Returns

A task that represents the asynchronous write operation.

Exceptions

buffer is null.

The index plus count is greater than the buffer length.

index or count is negative.

The text writer is disposed.

The text writer is currently in use by a previous write operation.

WriteAsync(String)

Writes a string to the text string or stream asynchronously.

[HostProtection(ExternalThreading = true)]
[ComVisible(false)]
public virtual Task WriteAsync(string value)
Parameters
value
String

The string to write. If value is null, nothing is written to the text stream.

Returns

A task that represents the asynchronous write operation.

Exceptions

The text writer is disposed.

The text writer is currently in use by a previous write operation.

WriteLine()

Writes a line terminator to the text string or stream.

public virtual void WriteLine()
Exceptions

An I/O error occurs.

WriteLine(Boolean)

Writes the text representation of a Boolean value followed by a line terminator to the text string or stream.

public virtual void WriteLine(bool value)
Parameters
value
Boolean

The Boolean value to write.

Exceptions

An I/O error occurs.

WriteLine(Char)

Writes a character followed by a line terminator to the text string or stream.

public virtual void WriteLine(char value)
Parameters
value
Char

The character to write to the text stream.

Exceptions

An I/O error occurs.

WriteLine(Char[])

Writes an array of characters followed by a line terminator to the text string or stream.

public virtual void WriteLine(char[] buffer)
Parameters
buffer
Char[]

The character array from which data is read.

Exceptions

An I/O error occurs.

WriteLine(Char[], Int32, Int32)

Writes a subarray of characters followed by a line terminator to the text string or stream.

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

The character array from which data is read.

index
Int32

The character position in buffer at which to start reading data.

count
Int32

The maximum number of characters to write.

Exceptions

The buffer length minus index is less than count.

The buffer parameter is null.

index or count is negative.

An I/O error occurs.

WriteLine(Decimal)

Writes the text representation of a decimal value followed by a line terminator to the text string or stream.

public virtual void WriteLine(decimal value)
Parameters
value
Decimal

The decimal value to write.

Exceptions

An I/O error occurs.

See Also

WriteLine(Double)

Writes the text representation of a 8-byte floating-point value followed by a line terminator to the text string or stream.

public virtual void WriteLine(double value)
Parameters
value
Double

The 8-byte floating-point value to write.

Exceptions

An I/O error occurs.

See Also

WriteLine(Int32)

Writes the text representation of a 4-byte signed integer followed by a line terminator to the text string or stream.

public virtual void WriteLine(int value)
Parameters
value
Int32

The 4-byte signed integer to write.

Exceptions

An I/O error occurs.

See Also

WriteLine(Int64)

Writes the text representation of an 8-byte signed integer followed by a line terminator to the text string or stream.

public virtual void WriteLine(long value)
Parameters
value
Int64

The 8-byte signed integer to write.

Exceptions

An I/O error occurs.

See Also

WriteLine(Object)

Writes the text representation of an object by calling the ToString method on that object, followed by a line terminator to the text string or stream.

public virtual void WriteLine(object value)
Parameters
value
Object

The object to write. If value is null, only the line terminator is written.

Exceptions

An I/O error occurs.

See Also

WriteLine(Single)

Writes the text representation of a 4-byte floating-point value followed by a line terminator to the text string or stream.

public virtual void WriteLine(float value)
Parameters
value
Single

The 4-byte floating-point value to write.

Exceptions

An I/O error occurs.

See Also

WriteLine(String)

Writes a string followed by a line terminator to the text string or stream.

public virtual void WriteLine(string value)
Parameters
value
String

The string to write. If value is null, only the line terminator is written.

Exceptions

An I/O error occurs.

WriteLine(String, Object)

Writes a formatted string and a new line to the text string or stream, using the same semantics as the Format(String, Object) method.

public virtual void WriteLine(string format, object arg0)
Parameters
format
String

A composite format string (see Remarks).

arg0
Object

The object to format and write.

Exceptions

format is null.

An I/O error occurs.

format is not a valid composite format string.

-or-

The index of a format item is less than 0 (zero), or greater than or equal to the number of objects to be formatted (which, for this method overload, is one).

See Also

WriteLine(String, Object, Object)

Writes a formatted string and a new line to the text string or stream, using the same semantics as the Format(String, Object, Object) method.

public virtual void WriteLine(string format, object arg0, object arg1)
Parameters
format
String

A composite format string (see Remarks).

arg0
Object

The first object to format and write.

arg1
Object

The second object to format and write.

Exceptions

format is null.

An I/O error occurs.

format is not a valid composite format string.

-or-

The index of a format item is less than 0 (zero), or greater than or equal to the number of objects to be formatted (which, for this method overload, is two).

See Also

WriteLine(String, Object, Object, Object)

Writes out a formatted string and a new line, using the same semantics as Format(String, Object).

public virtual void WriteLine(string format, object arg0, object arg1, object arg2)
Parameters
format
String

A composite format string (see Remarks).

arg0
Object

The first object to format and write.

arg1
Object

The second object to format and write.

arg2
Object

The third object to format and write.

Exceptions

format is null.

An I/O error occurs.

format is not a valid composite format string.

-or-

The index of a format item is less than 0 (zero), or greater than or equal to the number of objects to be formatted (which, for this method overload, is three).

See Also

WriteLine(String, Object[])

Writes out a formatted string and a new line, using the same semantics as Format(String, Object).

public virtual void WriteLine(string format, params object[] arg)
Parameters
format
String

A composite format string (see Remarks).

arg
Object[]

An object array that contains zero or more objects to format and write.

Exceptions

A string or object is passed in as null.

An I/O error occurs.

format is not a valid composite format string.

-or-

The index of a format item is less than 0 (zero), or greater than or equal to the length of the arg array.

See Also

WriteLine(UInt32)

Writes the text representation of a 4-byte unsigned integer followed by a line terminator to the text string or stream.

[CLSCompliant(false)]
public virtual void WriteLine(uint value)
Parameters
value
UInt32

The 4-byte unsigned integer to write.

Attributes
Exceptions

An I/O error occurs.

See Also

WriteLine(UInt64)

Writes the text representation of an 8-byte unsigned integer followed by a line terminator to the text string or stream.

[CLSCompliant(false)]
public virtual void WriteLine(ulong value)
Parameters
value
UInt64

The 8-byte unsigned integer to write.

Attributes
Exceptions

An I/O error occurs.

See Also

WriteLineAsync()

Writes a line terminator asynchronously to the text string or stream.

[HostProtection(ExternalThreading = true)]
[ComVisible(false)]
public virtual Task WriteLineAsync()
Returns

A task that represents the asynchronous write operation.

Exceptions

The text writer is disposed.

The text writer is currently in use by a previous write operation.

WriteLineAsync(Char)

Writes a character followed by a line terminator asynchronously to the text string or stream.

[HostProtection(ExternalThreading = true)]
[ComVisible(false)]
public virtual Task WriteLineAsync(char value)
Parameters
value
Char

The character to write to the text stream.

Returns

A task that represents the asynchronous write operation.

Exceptions

The text writer is disposed.

The text writer is currently in use by a previous write operation.

WriteLineAsync(Char[])

Writes an array of characters followed by a line terminator asynchronously to the text string or stream.

[HostProtection(ExternalThreading = true)]
[ComVisible(false)]
public Task WriteLineAsync(char[] buffer)
Parameters
buffer
Char[]

The character array to write to the text stream. If the character array is null, only the line terminator is written.

Returns

A task that represents the asynchronous write operation.

Exceptions

The text writer is disposed.

The text writer is currently in use by a previous write operation.

WriteLineAsync(Char[], Int32, Int32)

Writes a subarray of characters followed by a line terminator asynchronously to the text string or stream.

[HostProtection(ExternalThreading = true)]
[ComVisible(false)]
public virtual Task WriteLineAsync(char[] buffer, int index, int count)
Parameters
buffer
Char[]

The character array to write data from.

index
Int32

The character position in the buffer at which to start retrieving data.

count
Int32

The number of characters to write.

Returns

A task that represents the asynchronous write operation.

Exceptions

buffer is null.

The index plus count is greater than the buffer length.

index or count is negative.

The text writer is disposed.

The text writer is currently in use by a previous write operation.

WriteLineAsync(String)

Writes a string followed by a line terminator asynchronously to the text string or stream.

[HostProtection(ExternalThreading = true)]
[ComVisible(false)]
public virtual Task WriteLineAsync(string value)
Parameters
value
String

The string to write. If the value is null, only a line terminator is written.

Returns

A task that represents the asynchronous write operation.

Exceptions

The text writer is disposed.

The text writer is currently in use by a previous write operation.