Today's Question:  What are you most afraid of as a programmer?        GIVE A SHOUT

Technical Article => Programming =>  C#

Simple but Interesting Features of VS2010 and C# 4.0

  Anuja Pawar Indore      2011-11-18 08:59:29      1,402    0    0


VS 2010 and C# 4.0 introduced so many new features. Here in this article, I try to cover some very simple, yet very useful features of both.

1. Hiding the Selected Part of Code

Many a times, a situation arises when we want to hide a specified piece of code rather that hiding the entire region. This has become easier in VS 2010. Just select the part of the code that you want to hide and right click selectOutlining -> Hide Selection.

Same way like a region code also gets collapsible and expandable area. Anytime you wish to remove this hiding of text again, select right click, select Outlining -> Stop Hiding Current.

2. DataTips

This option provides us with a simpler way by which we can communicate with any variable at debugging time. In 2010, a DataTip can be pinned at a location or it can even float. This gives us an easier way to monitor our variable and its value during debugging. We can add as many DataTips and will remain there even after the session is closed.

In order to add Pin to DataTip, just place the mouse pointer over any variable you can see the pin icon click on it.

You can move your pin to any location. Pin provides you three buttons close, Unpin (to remove pin), Expand (to add any comment).

Once code has been debugged and still we want to see what value the variable/expression was holding at the time of debugging, then this feature is very helpful.

You can even Export/Import your DataTips. Click on Debug ->Export DataTips -> Specify the location where you want the XML file to be saved.

You can access your XML file anytime by selecting Import option on the Debug -> Import DataTips.

3. String.IsNullOrWhiteSpace

It checks whether the specified string is empty, null, or contains only white-space characters. If the stringcontains any of this, only then the method will return true.

  • \n” for new lines
  • ” \r” for carriage returns
  • \v” for vertical spaces
  • Normal spaces
  • \t” for tab characters

4. Named and Optional Parameters

VS2010 parameters become optional when default value is assigned to it.

In the above method, we can omit Val1 and Val2 as they have a default value and can be treated as optional. The moment we start tying the method, the intelligence indicates which parameters are optional.

Named arguments allow user not to remember the order of parameters. If you know the name of the parameter, then you can call them in any order. Intelligence supports named argument by parameter name followed by colon (:).

Here is a complete example showing optional and named argument:

5. Highlighting

Select an identifier anywhere in the code and all the places where that identifier is used gets highlighted.

In the below example, I have selected identifier Val2 and all its usages are highlighted automatically.

6. Intelligence Generated as Per Usage

This feature is quite interesting. I experienced it when I created a class with the name test and while declaring the object, by mistake I wrote tset and it allowed me to create the object with a red line.

When I saw a red line, I just right clicked my mouse and saw an option called generate having two choices Classand New type. The intelligence is improved so much that any method missing in the class can be used first and created later.

7. URL Routing

URL mapping was possible to a certain extent in ASP.NET 2.0. We can define the URL which we want to show but the problem was in the case of postback, the actual URL in the browser is shown.

URL routing was introduced in ASP.NET 3.5. In this problem of PostBack was solved but we had to create different handler classes depending on the number of URL routings.

In ASP.NET 4.0, the need for defining separate handler classes for each routing has been removed and there is a built in helper function named MapPageRoute which helps to implement routing more quickly. These routes are registeredonApplication_Start.

Let's do routing using a simple example. I have two pages default and default2. And a button on each page to take you to the next. Along with that, moving from one page to another, I need to pass a parameter too. First thing in the global.asax file under the Application_Start, we have to assign the URL that we want them to appear on the browser window.

void Application_Start(object sender, EventArgs e)
   // Code that runs on application startup

	("Route1", "MySecondPage/{Name}", "~/Default2.aspx");

default will have the URL MyfirstPage/Value of the parameter name and default2 will have the URLMySecongPage/value of the parameter name.

On the button click of default page, I have added the following code to call default2 page:

protected void btnBack_Click(object sender, EventArgs e)
    Response.Redirect(ResolveUrl("~/MySecondPage/") + "An");

Here "An" is the value of the parameter Name, and a similar code is added on default2 page to take you back todefault page.

protected void btnMove_Click(object sender, EventArgs e)
    Response.Redirect(ResolveUrl("~/MyFirstPage/") + "Test");

Routing has become much easier and simpler. We can pass more than one parameter too.

8. Dynamic Language Support

Dynamic languages are the ones which don't perform compile-time type checks, rather they identify the type of objects at run time only. It is faster and easier to write but we are not able to see compile time errors. So, we have to make sure that the application is behaving in the manner as depicted.

If we see previous versions of C# then it was fully static language, but 4.0 has added a new dynamic element to support dynamic feature. Using dynamic keyword means telling the compiler to turn off compile-time checking.

Now comes a very obvious question, what is the difference between ObjectVar and Dynamic types. Let's see:

Compiler has a little information about the type of the variableNo information with the compilerCompiler has complete information
The variable requires casting before being usedNo casting requiredNo casting required, as the compiler already has complete information
If we don't have more information of the data type, it is usefulWhen we need to write less code and using Dynamic languageUsually used with Linq


I have created a sample application using all the above mentioned features. This is not the end, just the beginning of my exploration of the VS2010 features. It has become really very interesting when trying to go deep in these features and there are many more to explore.




Share on Facebook  Share on Twitter  Share on Google+  Share on Weibo  Share on Reddit  Share on Digg  Share on Tumblr    Delicious



No comment for this article.


Wait! Which sort algorithm to choose?

By sonic0002
Wait! Which sort algorithm to choose? Quick sort? Bubble sort? Insertion sort? ....