Прочетете на английски Редактиране

Споделяне чрез


StringWriter.WriteLineAsync Method

Definition

Asynchronously writes data to the string, followed by a line terminator.

Overloads

WriteLineAsync(StringBuilder, CancellationToken)

Asynchronously writes the string representation of the string builder to the current string, followed by a line terminator.

WriteLineAsync(Char)

Asynchronously writes a character to the string, followed by a line terminator.

WriteLineAsync(String)

Asynchronously writes a string to the current string, followed by a line terminator.

WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken)

Asynchronously writes the string representation of the memory region of characters to the current string, followed by a line terminator.

WriteLineAsync(Char[], Int32, Int32)

asynchronously writes a subarray of characters to the string, followed by a line terminator.

WriteLineAsync(StringBuilder, CancellationToken)

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

Asynchronously writes the string representation of the string builder to the current string, followed by a line terminator.

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

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 WriteLine(StringBuilder).

Applies to

.NET 10 и други версии
Продукт Версии
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10

WriteLineAsync(Char)

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

Asynchronously writes a character to the string, followed by a line terminator.

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

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 WriteLineAsync(Char) method.

C#
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.WriteLineAsync(c);
                }
                Console.WriteLine(stringToWrite.ToString());
            }
        }
    }
}
// The example displays the following output:
//
// Characters in StringBuilder
// a
// n
// d
//
// c
// h
// a
// r
// s
//
// t
// o
//
// a
// d
// d
//

Remarks

The line terminator is defined by the NewLine property.

Applies to

.NET 10 и други версии
Продукт Версии
.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, 10
.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

WriteLineAsync(String)

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

Asynchronously writes a string to the current string, followed by a line terminator.

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

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

C#
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.WriteLineAsync("and add characters through StringWriter");
                Console.WriteLine(stringToWrite.ToString());
            }
        }
    }
}
// The example displays the following output:
//
// Characters in StringBuilder
// and add characters through StringWriter
//

Remarks

The line terminator is defined by the NewLine property.

Applies to

.NET 10 и други версии
Продукт Версии
.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, 10
.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

WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken)

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

Asynchronously writes the string representation of the memory region of characters to the current string, followed by a line terminator.

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

Parameters

buffer
ReadOnlyMemory<Char>

A memory region of characters 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

.NET 10 и други версии
Продукт Версии
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Standard 2.1

WriteLineAsync(Char[], Int32, Int32)

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

asynchronously writes a subarray of characters to the string, followed by a line terminator.

C#
public override System.Threading.Tasks.Task WriteLineAsync(char[] buffer, int index, int count);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task WriteLineAsync(char[] buffer, int index, int count);

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 WriteLineAsync(Char[], Int32, Int32) method.

C#
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.WriteLineAsync(charsToAdd, 0, charsToAdd.Length);

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

Remarks

The line terminator is defined by the NewLine property.

Applies to

.NET 10 и други версии
Продукт Версии
.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, 10
.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