TextWriter.WriteAsync Method

Definition

Writes data to the text stream asynchronously.

Overloads

WriteAsync(Char[], Int32, Int32)

Writes a subarray of characters to the text stream asynchronously.

WriteAsync(StringBuilder, CancellationToken)

Asynchronously writes a string builder to the text stream.

WriteAsync(ReadOnlyMemory<Char>, CancellationToken)

Asynchronously writes a character memory region to the text stream.

WriteAsync(String)

Writes a string to the text stream asynchronously.

WriteAsync(Char[])

Writes a character array to the text stream asynchronously.

WriteAsync(Char)

Writes a character to the text stream asynchronously.

WriteAsync(Char[], Int32, Int32)

Source:
TextWriter.cs
Source:
TextWriter.cs
Source:
TextWriter.cs

Writes a subarray of characters to the text stream asynchronously.

C#
public virtual System.Threading.Tasks.Task WriteAsync (char[] buffer, int index, int count);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public virtual System.Threading.Tasks.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.

Attributes

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.

Remarks

The TextWriter class is an abstract class. Therefore, you do not instantiate it in your code. For an example of using the WriteAsync method, see the StreamWriter.WriteAsync method.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Write(Char[], Int32, Int32).

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

WriteAsync(StringBuilder, CancellationToken)

Source:
TextWriter.cs
Source:
TextWriter.cs
Source:
TextWriter.cs

Asynchronously writes a string builder to the text stream.

C#
public virtual System.Threading.Tasks.Task WriteAsync (System.Text.StringBuilder? value, System.Threading.CancellationToken cancellationToken = default);

Parameters

value
StringBuilder

The string, as a string builder, to write to the text stream.

cancellationToken
CancellationToken

The token to monitor for cancellation requests. The default value is None.

Returns

A task that represents the asynchronous write operation.

Exceptions

The cancellation token was canceled. This exception is stored into the returned task.

Remarks

This method is equivalent to calling WriteAsync(stringBuilder.ToString()), however, it uses the StringBuilder.GetChunks() method to avoid creating the intermediate string.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Write(StringBuilder).

Applies to

.NET 9 and other versions
Product Versions
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9

WriteAsync(ReadOnlyMemory<Char>, CancellationToken)

Source:
TextWriter.cs
Source:
TextWriter.cs
Source:
TextWriter.cs

Asynchronously writes a character memory region to the text stream.

C#
public virtual System.Threading.Tasks.Task WriteAsync (ReadOnlyMemory<char> buffer, System.Threading.CancellationToken cancellationToken = default);

Parameters

buffer
ReadOnlyMemory<Char>

The character memory region to write to the text stream.

cancellationToken
CancellationToken

The token to monitor for cancellation requests. The default value is None.

Returns

A task that represents the asynchronous write operation.

Exceptions

The cancellation token was canceled. This exception is stored into the returned task.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

WriteAsync(String)

Source:
TextWriter.cs
Source:
TextWriter.cs
Source:
TextWriter.cs

Writes a string to the text stream asynchronously.

C#
public virtual System.Threading.Tasks.Task WriteAsync (string value);
C#
public virtual System.Threading.Tasks.Task WriteAsync (string? value);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public virtual System.Threading.Tasks.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.

Attributes

Exceptions

The text writer is disposed.

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

Remarks

The TextWriter class is an abstract class. Therefore, you do not instantiate it in your code. For an example of using the WriteAsync method, see the StreamWriter.WriteAsync method.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Write(String).

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

WriteAsync(Char[])

Source:
TextWriter.cs
Source:
TextWriter.cs
Source:
TextWriter.cs

Writes a character array to the text stream asynchronously.

C#
public System.Threading.Tasks.Task WriteAsync (char[] buffer);
C#
public System.Threading.Tasks.Task WriteAsync (char[]? buffer);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public System.Threading.Tasks.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.

Attributes

Exceptions

The text writer is disposed.

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

Remarks

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Write(Char[]).

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

WriteAsync(Char)

Source:
TextWriter.cs
Source:
TextWriter.cs
Source:
TextWriter.cs

Writes a character to the text stream asynchronously.

C#
public virtual System.Threading.Tasks.Task WriteAsync (char value);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public virtual System.Threading.Tasks.Task WriteAsync (char value);

Parameters

value
Char

The character to write to the text stream.

Returns

A task that represents the asynchronous write operation.

Attributes

Exceptions

The text writer is disposed.

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

Remarks

The TextWriter class is an abstract class. Therefore, you do not instantiate it in your code. For an example of using the WriteAsync method, see the StreamWriter.WriteAsync method.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Write(Char).

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0