C# StreamReader ReadToEndAsync

In this chapter you will learn:

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

Description

StreamReader ReadToEndAsync Reads all characters from the current position to the end of the stream asynchronously and returns them as one string.

Syntax

StreamReader.ReadToEndAsync has the following syntax.


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

Returns

StreamReader.ReadToEndAsync method returns <

Example

The following example shows how to read the contents of a file by using the ReadToEndAsync() method.


using System;/*from  www .  j  a v a2  s  . c o  m*/
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.ReadToEndAsync();
            Console.WriteLine(result);
        }
    }
}

Next chapter...

What you will learn in the next chapter:

  1. Get to know StreamWriter.Null
  2. Syntax for StreamWriter.Null
  3. Example - StreamWriter.Null