C# StreamReader ReadLineAsync

In this chapter you will learn:

  1. Get to know StreamReader.ReadLineAsync
  2. Syntax for StreamReader.ReadLineAsync
  3. Returns for StreamReader.ReadLineAsync
  4. Example - StreamReader.ReadLineAsync

Description

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

Syntax

StreamReader.ReadLineAsync has the following syntax.


[ComVisibleAttribute(false)]
[HostProtectionAttribute(SecurityAction.LinkDemand, ExternalThreading = true)]
public override Task<string> ReadLineAsync()

Returns

StreamReader.ReadLineAsync method returns <

Example

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


/* ww  w . j  a  v a2  s.c  o m*/
using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        ReadCharacters();
    }

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

Next chapter...

What you will learn in the next chapter:

  1. Get to know StreamReader.ReadToEnd
  2. Syntax for StreamReader.ReadToEnd
  3. Returns for StreamReader.ReadToEnd
  4. Example - StreamReader.ReadToEnd