C# IntelliBits: New Features in C# 5.0 - Asynchronous Programming

In this installment of C# IntelliBits, we will look at the new features available to developers in C# 5.0. Check out my previous posts for some basic C# tips.

Note: C# 5.0 was released in Visual Studio 2012. Microsoft has since released Visual Studio 2013, which contained no new features for the C# language. For more information on this topic, please see the following blog from the Microsoft C# team.

C# IntelliBits #7


Applies to: .NET Framework versions 4.0, 4.5, 4.5.1

A nice new feature has been added for asynchronous programming in C# 5.0. This feature comes in the form of two new keywords—async and await—which are used to make a synchronous method asynchronous. With asynchronous programming, a method is called that runs in the background and the calling thread is not blocked.

Task (a keyword that was introduced in .NET Framework 4.0) is used to represent an asynchronous operation. It returns no value. A Task<int> returns an element of type int.


The async keyword is used to qualify a function, delegate or lambda expression as asynchronous. The async method can return only a void or a Task.


The await keyword is used to suspend the execution of an asynchronous function until the awaited task completes its operation. The await expression can only be used within an async method.

Let’s examine a simple program that explains these concepts in more detail.

Code sample:

//basic namespaces
using System;
using System.IO;
using System.Threading.Tasks;

class Program
    static void Main()
// create and start an asychronous task 
Task fileTask = new Task(ProcessDataFile);
//wait for the task completion

    //asynchronous method
    static async void ProcessDataFile()
// Start the HandleFile task.
Task<int> fileTask = HandleFile("C:\\RHTPayRoll.txt");

// Control returns here before HandleFile returns.
// Notify user of processing status
Console.WriteLine("Please wait, processing RHT payroll file.”);

// Wait for the HandleFile task to complete.
//  Display its results.
int taskValue = await fileTask;
Console.WriteLine("Count: " + taskValue);
    static async Task<int> HandleFile(string fileName)
Console.WriteLine("Entering HandleFile method.");
int fileCount = 0;

// Read in the specified file.
//  Use async StreamReader method.
using (StreamReader reader = new StreamReader(fileName))
    string fileReader = await reader.ReadToEndAsync();

    //Process the data file
    fileCount += fileReader.Length;

    // sample computation process.
    for (int i = 0; i < 10000; i++)
var hashCodeValue = fileReader .GetHashCode();
if (hashCodeValue == 0)
Console.WriteLine("Exit HandleFile Process.");
return fileCount;

Produces the following output:

Entering HandleFile method

Please wait, processing RHT payroll file.

Entering HandleFile method

Please wait, processing RHT payroll file.

Exit FileHandle Process

Count: 1916146


Asynchrony is an important new feature in the .NET Framework 4.5. The new async and await keywords enable you to provide noteworthy user experience improvements in your applications without much effort on your part.

I hope you have enjoyed this installment of C# IntelliBits. Next month we will be discussing Object Oriented Programming concepts relative to C#.

Have a great 2014!