StreamReader.ReadLineAsync Method

Definition

Overloads

ReadLineAsync()

Reads a line of characters asynchronously from the current stream and returns the data as a string.

ReadLineAsync(CancellationToken)

Reads a line of characters asynchronously from the current stream and returns the data as a string.

ReadLineAsync()

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

Reads a line of characters asynchronously from the current stream and returns the data as a string.

C#
public override System.Threading.Tasks.Task<string> ReadLineAsync();
C#
public override System.Threading.Tasks.Task<string?> ReadLineAsync();
C#
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task<string> ReadLineAsync();

Returns

A task that represents the asynchronous read operation. The value of the TResult parameter contains the next line from the stream, or is null if all the characters have been read.

Attributes

Exceptions

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

The stream has been disposed.

The reader is currently in use by a previous read operation.

Examples

The following example shows how to read the first line of a file by using the ReadLineAsync() method.

C#
using System;
using System.IO;

namespace ConsoleApplication
{
    class Program
    {
        static async Task Main()
        {
            await ReadCharacters();
        }

        static async Task ReadCharacters()
        {
            String result;
            using (StreamReader reader = File.OpenText("existingfile.txt"))
            {
                Console.WriteLine("Opened file.");
                result = await reader.ReadLineAsync();
                Console.WriteLine("First line contains: " + result);
            }
        }
    }
}

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

Applies to

.NET 10 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, 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

ReadLineAsync(CancellationToken)

Source:
StreamReader.cs
Source:
StreamReader.cs
Source:
StreamReader.cs

Reads a line of characters asynchronously from the current stream and returns the data as a string.

C#
public override System.Threading.Tasks.ValueTask<string?> ReadLineAsync(System.Threading.CancellationToken cancellationToken);

Parameters

cancellationToken
CancellationToken

The token to monitor for cancellation requests.

Returns

A value task that represents the asynchronous read operation. The value of the TResult parameter contains the next line from the stream, or is null if all of the characters have been read.

Exceptions

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

The stream reader has been disposed.

The reader is currently in use by a previous read operation.

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

Remarks

If this method is canceled via cancellationToken, some data that has been read from the current Stream but not stored (by the StreamReader) or returned (to the caller) may be lost.

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

Applies to

.NET 10 and other versions
Product Versions
.NET 7, 8, 9, 10