Labels

Monday, October 14, 2013

C# 5.0 New Features

 

 

Two New Features:

 

1.      Async Feature

2.      Caller Information

 

 

Async Feature:

 

Two new key words are used for Async feature: async modifier and await operator.

Method marked with async modifier is called async method.

 

private async void

btnTest_Click(object sender, EventArgs e)

{

            var request = WebRequest.Create(txtUrl.Text.Trim());

            var content = new MemoryStream();

 

            Task<WebResponse> responseTask = request.GetResponseAsync();

 

            //await operator to supends the excution of the method until the task is completed.

            //In the meantime, the control is returned the UI thread.

            using (var response = await responseTask)

            {

                        using (var responseStream = response.GetResponseStream())

                        {

                                    Task copyTask = responseStream.CopyToAsync(content);

                                    await copyTask;

                        }

            }

 

            txtResult.Text = content.Length.ToString();

}

 

The await operator is applied to the returned task. The await operator suspends execution of the method until the task is completed. Meanwhile, control is returned to the caller of the suspended method.

 

Note:

 

In the past, we can also use BeginGetResponse method to send async request as this sample in MSDN shows:
http://msdn.microsoft.com/zh-cn/library/system.net.httpwebrequest.begingetresponse(v=vs.80).aspx.

But it  will takes us a lot effort to realize it.

                                                                  

 

Caller Information:

 

Caller Information can help us in tracing, debugging and creating diagnose tools.

It will help us to avoid duplicate codes which are generally invoked in many methods for same purpose, such as logging and tracing.

 

We could get the below information of caller method :

 

 

Below example are a common practice prior to the new feature of Caller Information:

 

namespace ConsoleApplicationTest

{

            class Program

            {

                        static void Main(string[] args)

                        {

                                    InsertLog("Main");

                                    MethodB();

                                    Console.ReadLine();

                        }

 

                        static void MethodA()

                        {

                                    InsertLog("MethodA");

                                    MethodB();

                        }

 

                        static void MethodB()

                        { }

 

                        static void InsertLog(string methodName)

                        {

                                    Console.WriteLine("{0} called method B at {1}", methodName,DateTime.Now);

                        }

            }

}

 

In both Main and MethodA methods, method InsertLog is invoked for logging. Now we can change the

codes to be as per below lines:

 

namespace ConsoleApplicationTest

{

            class Program

            {

                        static void Main(string[] args)

                        {

                                    //InsertLog("Main");

                                    MethodB();

                                    Console.ReadLine();

                        }

 

                        static void MethodA()

                        {

                                    //InsertLog("MethodA");

                                    MethodB();

                        }

 

                        static void MethodB(

                        [CallerMemberName] string memberName = "",

                        [CallerFilePath] string sourceFilePath = "",

                        [CallerLineNumber] int sourceLineNumber = 0)

                        {

                                    InsertLog(memberName);

                        }

 

                        static void InsertLog(string methodName)

                        {

                                    Console.WriteLine("{0} called method B at {1}", methodName,DateTime.Now);

                        }

            }

}

 

 

Hope this helps.

 

Arun Manglick

 

 

 

 

 

No comments:

Post a Comment