Redigera

Dela via


StringWriter.WriteAsync Method

Definition

Writes data to the string asynchronously.

Overloads

WriteAsync(Char)

Writes a character to the string asynchronously.

WriteAsync(String)

Writes a string to the current string asynchronously.

WriteAsync(ReadOnlyMemory<Char>, CancellationToken)

Asynchronously writes a memory region of characters to the string.

WriteAsync(StringBuilder, CancellationToken)

Asynchronously writes the text representation of a string builder to the string.

WriteAsync(Char[], Int32, Int32)

Writes a subarray of characters to the string asynchronously.

WriteAsync(Char)

Source:
StringWriter.cs
Source:
StringWriter.cs
Source:
StringWriter.cs

Writes a character to the string asynchronously.

public:
 override System::Threading::Tasks::Task ^ WriteAsync(char value);
public override System.Threading.Tasks.Task WriteAsync (char value);
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task WriteAsync (char value);
override this.WriteAsync : char -> System.Threading.Tasks.Task
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.WriteAsync : char -> System.Threading.Tasks.Task
Public Overrides Function WriteAsync (value As Char) As Task

Parameters

value
Char

The character to write to the string.

Returns

A task that represents the asynchronous write operation.

Attributes

Exceptions

The string writer is disposed.

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

Examples

The following example shows how to write characters by using the WriteAsync(Char) method.

using System;
using System.Text;
using System.IO;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            WriteCharacters();
        }

        static async void WriteCharacters()
        {
            StringBuilder stringToWrite = new StringBuilder("Characters in StringBuilder");
            stringToWrite.AppendLine();

            using (StringWriter writer = new StringWriter(stringToWrite))
            {
                UnicodeEncoding ue = new UnicodeEncoding();
                char[] charsToAdd = ue.GetChars(ue.GetBytes("and chars to add"));
                foreach (char c in charsToAdd)
                {
                    await writer.WriteAsync(c);
                }
                Console.WriteLine(stringToWrite.ToString());
            }
        }
    }
}
// The example displays the following output:
//
// Characters in StringBuilder
// and chars to add
//
Imports System.IO
Imports System.Text

Module Module1

    Sub Main()
        WriteCharacters()
    End Sub

    Async Sub WriteCharacters()
        Dim stringToWrite As StringBuilder = New StringBuilder("Characters in StringBuilder")
        stringToWrite.AppendLine()

        Using writer As StringWriter = New StringWriter(stringToWrite)

            Dim ue As UnicodeEncoding = New UnicodeEncoding()
            Dim charsToAdd() = ue.GetChars(ue.GetBytes("and chars to add"))
            For Each c As Char In charsToAdd
                Await writer.WriteAsync(c)
            Next
            Console.WriteLine(stringToWrite.ToString())
        End Using
    End Sub
End Module
' The example displays the following output:
'
' Characters in StringBuilder
' and chars to add
'

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).

Applies to

WriteAsync(String)

Source:
StringWriter.cs
Source:
StringWriter.cs
Source:
StringWriter.cs

Writes a string to the current string asynchronously.

public:
 override System::Threading::Tasks::Task ^ WriteAsync(System::String ^ value);
public override System.Threading.Tasks.Task WriteAsync (string value);
public override System.Threading.Tasks.Task WriteAsync (string? value);
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task WriteAsync (string value);
override this.WriteAsync : string -> System.Threading.Tasks.Task
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.WriteAsync : string -> System.Threading.Tasks.Task
Public Overrides Function WriteAsync (value As String) As Task

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 string writer is disposed.

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

Examples

The following example shows how to write a string by using the WriteAsync(String) method.

using System;
using System.Text;
using System.IO;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            WriteCharacters();
        }

        static async void WriteCharacters()
        {
            StringBuilder stringToWrite = new StringBuilder("Characters in StringBuilder");
            stringToWrite.AppendLine();

            using (StringWriter writer = new StringWriter(stringToWrite))
            {
                await writer.WriteAsync("and add characters through StringWriter");
                Console.WriteLine(stringToWrite.ToString());
            }
        }
    }
}
// The example displays the following output:
//
// Characters in StringBuilder
// and add characters through StringWriter
//
Imports System.IO
Imports System.Text

Module Module1

    Sub Main()
        WriteCharacters()
    End Sub

    Async Sub WriteCharacters()
        Dim stringToWrite As StringBuilder = New StringBuilder("Characters in StringBuilder")
        stringToWrite.AppendLine()

        Using writer As StringWriter = New StringWriter(stringToWrite)
            Await writer.WriteAsync("and add characters through StringWriter")
            Console.WriteLine(stringToWrite.ToString())
        End Using
    End Sub
End Module
' The example displays the following output:
'
' Characters in StringBuilder
' and add characters through StringWriter
'

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(String).

Applies to

WriteAsync(ReadOnlyMemory<Char>, CancellationToken)

Source:
StringWriter.cs
Source:
StringWriter.cs
Source:
StringWriter.cs

Asynchronously writes a memory region of characters to the string.

public override System.Threading.Tasks.Task WriteAsync (ReadOnlyMemory<char> buffer, System.Threading.CancellationToken cancellationToken = default);
override this.WriteAsync : ReadOnlyMemory<char> * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Overrides Function WriteAsync (buffer As ReadOnlyMemory(Of Char), Optional cancellationToken As CancellationToken = Nothing) As Task

Parameters

buffer
ReadOnlyMemory<Char>

The character memory region to write to the string.

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

WriteAsync(StringBuilder, CancellationToken)

Source:
StringWriter.cs
Source:
StringWriter.cs
Source:
StringWriter.cs

Asynchronously writes the text representation of a string builder to the string.

public override System.Threading.Tasks.Task WriteAsync (System.Text.StringBuilder? value, System.Threading.CancellationToken cancellationToken = default);
override this.WriteAsync : System.Text.StringBuilder * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Overrides Function WriteAsync (value As StringBuilder, Optional cancellationToken As CancellationToken = Nothing) As Task

Parameters

value
StringBuilder

The string builder to write to the string.

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 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

WriteAsync(Char[], Int32, Int32)

Source:
StringWriter.cs
Source:
StringWriter.cs
Source:
StringWriter.cs

Writes a subarray of characters to the string asynchronously.

public:
 override System::Threading::Tasks::Task ^ WriteAsync(cli::array <char> ^ buffer, int index, int count);
public override System.Threading.Tasks.Task WriteAsync (char[] buffer, int index, int count);
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task WriteAsync (char[] buffer, int index, int count);
override this.WriteAsync : char[] * int * int -> System.Threading.Tasks.Task
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.WriteAsync : char[] * int * int -> System.Threading.Tasks.Task
Public Overrides Function WriteAsync (buffer As Char(), index As Integer, count As Integer) As Task

Parameters

buffer
Char[]

The character array to write data from.

index
Int32

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

count
Int32

The maximum 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 string writer is disposed.

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

Examples

The following example shows how to write characters by using the WriteAsync(Char[], Int32, Int32) method.

using System;
using System.Text;
using System.IO;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            WriteCharacters();
        }

        static async void WriteCharacters()
        {
            StringBuilder stringToWrite = new StringBuilder("Characters in StringBuilder");
            stringToWrite.AppendLine();

            using (StringWriter writer = new StringWriter(stringToWrite))
            {
                UnicodeEncoding ue = new UnicodeEncoding();
                char[] charsToAdd = ue.GetChars(ue.GetBytes("and chars to add"));

                await writer.WriteAsync(charsToAdd, 0, charsToAdd.Length);

                Console.WriteLine(stringToWrite.ToString());
            }
        }
    }
}
// The example displays the following output:
//
// Characters in StringBuilder
// and chars to add
//
Imports System.IO
Imports System.Text

Module Module1

    Sub Main()
        WriteCharacters()
    End Sub

    Async Sub WriteCharacters()
        Dim stringToWrite As StringBuilder = New StringBuilder("Characters in StringBuilder")
        stringToWrite.AppendLine()

        Using writer As StringWriter = New StringWriter(stringToWrite)

            Dim ue As UnicodeEncoding = New UnicodeEncoding()
            Dim charsToAdd() = ue.GetChars(ue.GetBytes("and chars to add"))

            Await writer.WriteAsync(charsToAdd, 0, charsToAdd.Length)

            Console.WriteLine(stringToWrite.ToString())
        End Using
    End Sub
End Module
' The example displays the following output:
'
' Characters in StringBuilder
' and chars to add
'

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[], Int32, Int32).

Applies to