Friday, September 16, 2011

Quiz: C# Async and Await. What is the Output of this Code?

C# 5.0 (CTP version at this point) has support for async and await to allow clean implementation of non-blocking IO using a task-continuation model. I was just playing with it and here's the sample code to tease your brain. Tell me what is the output of this program?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading.Tasks;
namespace AsyncCSharp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Starting...");
var smsohanDotCom = GetSmSohanDotCom();
Console.WriteLine("Passed the get line, status {0}", smsohanDotCom.IsCompleted);
Console.WriteLine(smsohanDotCom.Result.Length);
Console.WriteLine("Printed the length, status {0}", smsohanDotCom.IsCompleted);
Console.ReadLine();
}
private async static Task<String> GetSmSohanDotCom(){
Console.WriteLine("Before Waiting...");
var data = await new WebClient().DownloadStringTaskAsync(new Uri("http://smsohan.com"));
Console.WriteLine("Waiting...");
return data;
}
}
}