Techbubbles

Asynchronous Lambda Expressions in C#5

Asynchronous functions make asynchronous programming similar to synchronous programming. You can read my previous post on C# 5.0 features. Normal methods can written as asynchronous as shown below

   1: async Task Demo()

   2: {

   3: await Task.Delay(10000);

   4: Console.WriteLine("Hello World!!");

   5: }

You can call the method as await Demo();

Lambda Expressions and anonymous methods can be preceded by the async word.

   1: Func<Task> Ldemo = async () =>

   2:  { 

   3:    await Task.Delay(10000); 

   4:    Console.WriteLine ("Hello World!!");

   5:  };


You can call the method as await Ldemo();

Asynchronous Lambda expressions can also be used in attaching event handlers:

   1: btnOK.Click +=  async (sender, args) => {await Task.Delay(10000);btnOK.text = "Demo";};

Asynchronous Lambda expressions can return Task<TResult>:
   1: Func<Task<int>> Demo = async () =>

   2: {

   3:   await Task.Delay(10000);

   4:   return 12345;

   5: };

Share this post :

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Asynchronous Programming Recent comment authors
  Subscribe  
Notify of
Asynchronous Programming
Guest

Thank you for providing such information. This is very generous of you providing such vital Information which is

very informative.