Introduction of New Features in C# 5.0

The new features in C# 5.0 will help us to code more easily and improve the productivity. Have a nice
experience with the new release of Visual Studio 11!

1. C# Evolution Matrix

Microsoft just published a new version of C# : 5.0 beta with CLR version 4.5 (Visual Studio 11 beta).
In order to get a big picture of the whole evolution of C# language, I summarized all the key features
into a C# Evolution Matrix for your reference as below diagram shows:



In C# version 5.0, there are two key features: Async Programming and Caller Information.

2. 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. This new feature will help us a lot in async programming.
For example, in the programming of Winform, the UI thread will be blocked while we use
HttpWebRequest synchronously request any resource in the Internet. From the perspective of user
experience, we cannot interact with the form before the request is done.

private void
btnTest_Click(object sender, EventArgs e)

{
var request = WebRequest.Create(txtUrl.Text.Trim());
var content=new MemoryStream();
using (var response = request.GetResponse())
{
using (var responseStream = response.GetResponseStream())
{
responseStream.CopyTo(content);
}
}
txtResult.Text = content.Length.ToString();
}


In the above example, after we clicked the Test button, we cannot not make any change to the form

before the txtResult textbox shows the result.

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.

Now, we can simply use below code to do request asynchronously :

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();
using (var response = await responseTask)
{using (var responseStream = response.GetResponseStream())

{

Task copyTask = responseStream.CopyToAsync(content);

//await operator to supends the excution of the method until the task is completed. In the meantime, the control is returned the UI thread.

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.

3. 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:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

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);

}

}

}

Advertisements

The Best Site to Get JavaScript Code

The Best Site to Get JavaScript Code

You can get Scripts that will execute in your application as you needed with free of cost.

Yamaha Gladiator

Yamaha Gladiator

VEHICLE SUMMARY
Name: Gladiator
Type: Commuter
Top Speed: 108kph

ENGINE SPECIFICATIONS
Engine: 123.7 cc
Maximum Power: 10.8ps@7500rpm
Maximum Torque: 10.4nm@6500rpm
Gears: 5 Speed
Clutch: 160
Bore: NA
Stroke: NA
Cylinder Configuration: NA
Engine Block Material: NA
Chassis Type: NA
Carburetor: NA

DIMENSIONS
Length: N/A
Width: N/A
Height: N/A
OTHER SPECIFICATIONS
Weight: 127.00 kg
Ground Clearance: 160.00 mm
Fuel Tank: 13.00 ltrs
Wheelbase: 1300.00 mm
Electrical System: NA
Headlamp: NA
Horn: NA
Tubeless:
Colors: NA

NASA – Curiosity Lands on Mars

Curiosity is healthy as it continues to familiarize itself with its new home in Gale Crater and check out its systems. The team’s plans for Curiosity checkout today included raising the rover’s mast and continued testing of its high-gain antenna, whose pointing toward Earth will be adjusted on Sol 2. Science data were collected from Curiosity’s Radiation Assessment Detector, and activities were performed with the Rover Environmental Monitoring Station instrument. Curiosity transmitted its first color image from the surface of Mars, from the Mars Hand Lens Imager, or MAHLI, showing part of the north rim of Gale Crater. Additional calibration images were received from Curiosity’s Navcam and Mastcam. All systems are go for deployment of the rover’s remote sensing mast on Sol 2, followed by a 360-degree pan by the rover’s Navcam. The Mastcam will also be calibrated against a target image on the rover. NASA’s Mars Reconnaissance Orbiter returned a spectacular image of Curiosity’s landing site, depicting the rover, parachute, back shell, heat shield and descent stage. Data were received from both NASA’s Mars Reconnaissance Orbiter and Mars Odyssey.

Hello world!

Welcome to WordPress.com! This is your very first post. Click the Edit link to modify or delete it, or start a new post. If you like, use this post to tell readers why you started this blog and what you plan to do with it.

Happy blogging!