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?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |