New Features in Microsoft Visual Studio 2010 and the .NET Framework 4.0

Visual Studio 2008 may be better than sliced bread, but the development team at Microsoft has already been working on the next release. They have recently given us Visual Studio 2010 and the .NET Framework 4.0 as a Community Technology Preview (CTP); it boasts several features that would appeal to developers.

This article won't go into every single feature, but will go into features that are the most relevant to .NET developers. Please note that because this is a CTP, it doesn't mean that the final release will be exactly as you see in the CTP or as is described here. I can go over the features roughly as follows:

New Features in the Visual Studio 2010 IDE and .NET Framework 4.0

  • Call Hierarchy of methods
  • A New Quick Search
  • Multi-targeting more accurate
  • Parallel Programming and Debugging
  • XSLT Profiling and Debugging
  • The XSD Designer

New ASP.NET features

  • Static IDs for ASP.NET Controls
  • The Chart control
  • Web.config transformation

New VB.NET features

  • Auto Implemented Properties for VB.NET
  • Collection Initializers
  • Implicit Line Continuations
  • Statements in Lambda Expressions

New C# features

  • Dynamic Types
  • Optional parameters
  • Named and Optional Arguments

Conclusion and Resources

New Features in the Visual Studio 2010 IDE and .NET Framework 4.0

Call Hierarchy of Methods

In complicated solutions, a single method may be used from several different places, and attempting to follow how a particular method is being called can be difficult. Call hierarchy attempts to address this problem by visually presenting the flow of method calls to and from the method you are looking at. In other words, you can look at what calls your method and what your method calls in a treeview format.

Obviously, I cannot present a truly complex example, but a simple example should help illustrate the point.

protected void Page_Load(object sender, EventArgs e)
{
   BindDataControls()
}

private void BindDataControls()
{
   //DataBinding here
}

protected void Button1_Click(object sender, EventArgs e)
{
   BindDataControls();
}

Now, if you wanted to figure out what calls BindDataControls(), you can right-click on it and choose "View Call Hierarchy."

This gives you a window with a treeview format, as shown below, with nodes that you can expand ad infinitum (or until your machine runs out of memory). You also can right-click on the method names and go to their definition, or you can reset those methods as the root of the Call Hierarchy window if you want to work your way from there and don't care about other methods anymore Further, you can view call hierarchies from the object browser too, so you needn't always be viewing the code. This is a helpful visual cue for very complicated projects that we've all worked on at some point or another.

Visual Studio 2010: View Call Hierarchy
Figure 1

A New Quick Search

A nifty little feature that Microsoft has added is the Quick Search window. This isn't the same as the Search or Search and Replace window that searches for specific textual strings. It's different in the sense that it searches across symbols (methods, properties, and class names) across your solution and filters them in the result view for you.

In this example, I typed in 'but' and it brought back all symbols that contained 'but' regardless of position. You can specify multiple words to search for by separating them with a space.

Visual Studio 2010: Quick Search window
Figure 2

Multi-targeting more accurate

Although VS 2008 supports targeting different frameworks from the same IDE, one problem was that the Toolbox and Intellisense displayed types that were available to the .NET 3.5 Framework whether or not you were working with a .NET 3.5 project. This may have caused problems when you tried to use something, only to realize that it wasn't actually available to you.

Visual Studio 2010 gets smarter by only displaying items that you can use in the Toolbox and Intellisense (and other relevant areas).

Further, if you change your project to use a framework version that isn't available on your machine, you will be prompted and can choose to retarget to another version or download the target framework that you wanted.

Parallel Programming and Debugging

Don't let the name intimidate you. I think Microsoft has done a great job of allowing you to take advantage of multi-processor systems with very easy syntax to enable parallel programming that you can quickly adapt to.

In addition, to make things easier, Visual Studio 2010 comes with a set of visual tools that will help you debug and view simultaneously running threads. This means that you can view the task instances and the call stacks for each task in parallel.

If you've been using the Parallel Extensions to the .NET Framework, then most of this will be familiar to you.

A Task.StartNew will kick off your threads for you. If you then hit a breakpoint and view the Visual Studio 2010 Threads window, you can view the state of each thread and where it is currently. This can be seen below—the green indicating the main thread and the yellow indicating all the worker threads spawned.

Visual Studio 2010 Threads window
Figure 3

There will also be a Multistack window that allows you to peruse the call stacks of all of the threads currently being executed; this is again a helpful visual cue that groups together tasks sharing a stack frame.

Parallel Programming itself becomes a feature of the .NET Framework 4.0 as opposed to it being an extension right now. With Parallel Programming, you get Parallel LINQ (PLINQ) and syntax to make parallelization of algorithms easier. In fact, it's unbelievably easy.

Say you have a for loop that performed a complicated task:

for (int i = 0; i < 10; i++)
{
   DoSomethingReallyComplicated(i);
}

Assuming DoSomethingReallyComplicated does something really complicated (as the name implies), you could parallelize it by using Parallel.For and enclosing the iteration in a lambda expression.

Parallel.For(0, 10, i => { DoSomethingReallyComplicated(i); });

Similarly, there is Parallel.ForEach<> for foreach loops. You could also use Parallel LINQ to do the same thing. Taking a theoretical, basic LINQ equivalent to the above, you would get this:

from i in Enumerable.Range(0,9)
   where DoSomethingAndReturnAValue(i)
   select i

PLINQ would involve a very slight change; just add AsParallel():

from i in Enumerable.Range(0,10).AsParallel()
   where DoSomethingAndReturnValue(i)
   select i

You should be able to see the real effects of these changes on multi-core machines under intensive circumstances. Of course, there is much more than I've gotten into here, such as parallel profiling views, but that's beyond the scope here. You could get started now, if you'd like, by downloading the Parallel FX CTP.

XSLT Profiling and Debugging

Everyone hates XSLT. If you don't hate XSLT, you haven't used it enough. Part of this attitude towards XSLT stems from the difficulty you face when debugging it—it is a huge unknown and can blow up in your faces when you least expect it.

Visual Studio 2010 will offer an XSLT profiler to help with writing XSLT in the context of profiling and optimization. After writing your XSLT, you can use the "Profile XSLT" option in Visual Studio to supply it with a sample XML file that gets used for the analysis.

I decided to test out a sample XSLT from the Gallery of Stupid XSL and XSLT Tricks page because they would undoubtedly be complicated. The XSLT Profile on Compute square roots using the Babylonian Method produced this:

The XSLT Profile on Compute square roots using the Babylonian Method
Figure 4

Clicking on the 'offending' functions then takes you to the function details, among other views on the analysis available to you. Available to you are the paths of execution taken, assembly-level view of the execution, call hierarchy, statistics by function, and function details.

Visual Studio 2010 function details
Figure 5

Also available to you in the menu options is XSLT Debugging. You can launch an XSLT file with sample XML and step through it as you would normal managed code.

Visual Studio 2010 XSLT Debugging
Figure 6

No, your eyes aren't deceiving you. You can set breakpoints, have locals set, and step through the template functions as you please. This should help alleviate your collective fears of XSLT immensely.

The XSD Designer

While I'm on the subject of XML, VS 2010 also introduces a new XSD Designer.

Visual Studio 2010 XSD Designer
Figure 7

It comes with a schema explorer, a visual view of the relationships in different levels of detail, and sample XML generation.

New Features in Microsoft Visual Studio 2010 and the .NET Framework 4.0

New ASP.NET Features

Static IDs for ASP.NET Controls

A few much-needed features make their way to ASP.NET in the .NET Framework 4.0. You now have the ability to specify the ClientID that gets rendered to the page instead of having to fiddle with viewstate or hidden fields to manage the IDs that get generated out to the page. If you've ever had to use the .ClientID property of a control or found yourself using ClientScript.RegisterStartupScript or ClientScript.RegisterClientScriptBlock, you will know what I mean; it doesn't always feel good to have to write all your JavaScript in the codebehind. It'd be nice to let the JavaScript sit on the page or in an external JavaScript file—where it belongs—so that a change in the JavaScript doesn't require a recompilation of the website, while at the same time still using dynamic ASP.NET controls.

With ASP.NET 4.0, you now can create controls that support static Client IDs. To do this, simply get your control to implement INamingContainer. When using the control on the page, set its ClientIDMode property to Static.

For example, here is the codebehind for a simple web user control (ASCX) with a label in it.

public partial class WebUserControl1 : System.Web.UI.UserControl,
   INamingContainer
{
   protected void Page_Load(object sender, EventArgs e)
   {
      this.ClientIDMode = ClientIDMode.Static;
   }
}

You would need to expose the ClientIDMode property so that the parent page can set it. That's all you need to do, and the control gets rendered to the page with an ID of Label1.

When it comes to databound controls, though, you'd have to do things a little differently; it's really not acceptable to have more than one control with the same ID on a page. For this purpose, the ClientIDMode can be set to predictable and you also can specify a suffix that ensures that the IDs generated are predictable (but not fixed).

An example of its usage would be (in a gridview):

<asp:GridView ID="GridViewTest" runat="server"
              ClientIDMode="Predictable" RowClientIDSuffix="Pizza">
 ...
</asp:GridView>

And any controls rendered in the gridview should have IDs like "GridView1_PizzaLabelTest_1".

The Chart Control

The Chart control is finally here! This control sports various chart types, including point, column, bar, area, doughnut, range, and types I hadn't even heard of before such as Renko and Kagi.... The chart also has scale breaks for 'excessive' data, and even can be rendered in a 3D mode. As a quick test, I got to make these charts based on the meteor consumption of my friends:

[08_aspnetchart.jpg]
Figure 8

[09_aspnetchart.jpg]
Figure 9

Combined with ASP.NET AJAX functionality, this can open up a vast array of presentation configurations for your website if you deal with such data.

The chart works off an HTTP Image Handler. This means that you also have the option of plugging in your own charting image handling assembly if you want to customize it further.

Web.config transformation

In enterprise environments, your ASP.NET application will have to go across various stages of deployment such as testing, staging, preproduction, and production. Due to the relatively isolated nature of these environments, you often will have had to create separate web.config files for each environment and ensure that any changes you make to one web.config has made it to the other web.configs as well.

VS Team System 2010 has implemented a web.config transformation feature that can perform the value-based transformation that you need when deploying to different environments. The transformation is performed as part of an MS Build task, but you will need to specify the transforms to perform across the various web.configs; this is done by adding a set of xdt:Transform attributes to various nodes that may need changing (such as the obvious connection string).

An example of this would be:

<add name="theSiteDB"
     connectionString="Server=PreProduction;Database=mySite;
                       User Id=hello;password=world"
     providerName="System.Data.SqlClient" xdt:Transform="Replace"
     xdt:Locator="Match(name)"/>

Because of the XML-based nature of these transforms, it therefore is also possible to perform the transformation manually via the VS 2010 GUI or create deployment packages for the various environments that require minimal human intervention.

New Features in Microsoft Visual Studio 2010 and the .NET Framework 4.0

New VB.NET Features

Auto implemented properties

Often, you find yourselves declaring public properties like this:


public string DoorColor
{
   get
   {
      return doorColorValue;
   }
   set
   {
      doorColorValue = value;
   }
}

In VB.NET, this would be:

Public Property DoorColor As String
   Get
      Return _doorColor
   End Get
   Set(ByVal value As String)
      _doorColor = value
   End Set
End Property

More often than not, no extra logic ever gets placed in the get/set blocks. This is why C# was given an auto implemented property in which the private variable is declared behind the scenes for you.

public string DoorColor
{
   get; set;
}

This feature was not present for VB.NET in .NET Framework 3.5, but is now available in .NET Framework 4.0. It's as simple as this:

Public Property DoorColor As String

Collection Initializers

The syntax to initialize collections in VB.NET 10 (in .NET Framework 4.0) is now slightly shorter.

Dim breakfast = {New Crumpets With {.CrumpetAge = 99,
                                    .CrumpetSmell = "Bad"},
   New Crumpets With {.CrumpetAge = 29, .CrumpetSmell = "Good"}}

Note that you don't need to specify the collection type because it is implicitly understood. In .NET 3.5, this would not have compiled because it would have understood 'breakfast' as type 'Object' instead of Crumpets. .NET 4.0 understands Crumpets, a statement I never thought I'd actually write down.

Implicit Line Continuations

C# has had this for a long time—long lines of code can be split across several lines for more readability. VB.NET has had it, but you've always had to add an underscore (_) at the end of each line, which could get a little annoying. Certain types of statements can now be split across several lines without the _ required.

Therefore, the collection initializer example from the previous section can be declared like this:

Dim breakfast =
   {
      New Crumpets With {
                    .CrumpetAge = 221,
                    .CrumpetSmell = "Foul"
                    },
      New Crumpets With {
                    .CrumpetSmell = "good",
                    .CrumpetAge = 1
                    }
   }

It works on Console.WriteLine, too.

Console.WriteLine(
            DoorColor)

And with nested method calls.

Console.WriteLine(
   breakfast(
      1
   ).GetSomeString(
   99)
   )

Changes to Lambda Expressions: statements and subs

In VB.NET 9 (.NET Framework 3.5), Lambda Expressions always needed to return a value regardless of whether it was required or not. This could often cause confusion when switching between C# and VB.NET. The .NET Framework 4.0 addresses this issue by allowing VB.NET to create Lambda Expressions that don't return anything. And, Lambda Expressions can now also contain statements instead of having to pass the logic to other methods as you had to do previously.

To write expressions that return nothing:

Dim lambdaNothing = Sub() Nothing
Dim lambdaSomething = Sub(Message) Console.WriteLine(Message)

To write expressions that contain statements:

Dim lambdaReturn = Function(i)
   If i >= 100 Then
      Return 3
   ElseIf i >= 10 AndAlso i < 100 Then
      Return 2
   Else
      Return 1
   End If
End Function

New Features in Microsoft Visual Studio 2010 and the .NET Framework 4.0

New C# Features

C# Dynamic Types and Dynamic Programming

C# should now support a new static type called 'dynamic'. This essentially allows for dynamic dispatch or late binding of the variable in question. For example, suppose you have two simple classes with a common-name method:

public class Coffee
{
   public int GetZing()
   {
      return 1;
   }
}

public class Juice
{
   public string GetZing()
   {
      return "Orange";
   }
}

You also have a method that returns one of the two object types:

private Object GetOneOfThem(int i)
{
   if (i > 10)
   {
      return new Juice();
   }
   else
   {
      return new Coffee();
   }
}

You can then make a call to GetOneOfThem() without knowing what type you're going to have returned, but you can still attempt to call method names on it.

dynamic drink = GetOneOfThem(someVariable);
Console.WriteLine(drink.GetZing());

At runtime, GetOneOfThem() evaluates to either Juice() or Coffee() and then the corresponding method is called based on what the compiler resolves 'drink' to be. The above is a very simple example of dynamic programming—there is more to this. The underlying implementation of dynamic programming is the DLR—the Dynamic Language Runtime—which is what allows you to invoke things in a dynamic fashion. You can read more about dynamic programming and other new features in C# .

C# Optional Parameters

VB.NET has had this for a long time, and now C# gets it too—optional parameters in your function signatures.

private string GetMeaninglessDrivel(string drivelSeed="bork")
{
   //...
}

In the past, the lack of optional parameters in C# was overcome by overloading the method several times and calling the same common method from all overloads with different values being passed in. Optional parameters can help prevent unnecessary multiple overloads of methods and making a class unnecessarily complex.

Named and Optional Arguments

This feature is somewhat related to Optional Parameters. Suppose you have a method that has several optional parameters.

private void SaveTheseValues(int i=1, int j=2, int k=3)
{
   //...
}

If you wanted to pass in values just for j and k, but not i, you can now name the arguments that you are passing in instead of passing in values and skipping values with multiple commas.

So, instead of this:

SaveTheseValues(,5,4)

You can do this:

SaveTheseValues(j:5,k:4)

New Features in Microsoft Visual Studio 2010 and the .NET Framework 4.0

Conclusion and Resources

That was a quick overview of what will probably be available to you in Visual Studio 2010 and the .NET Framework 4.0. To reiterate, you must keep in mind that because it is a CTP, not everything that you see will be exactly as it is when it is finally released (which is why I often discourage books that come out at the same time as the Visual Studio release - they're based on Betas and CTPs and so will not be completely reliable)

You can have a look at VS 2010 yourself if you'd like and if you can meet the hefty requirements for the Virtual PC image. You will need about 70 to 80 GB of hard disk space available, 2GB of RAM and a dual-core system at the least. Download Visual Studio 2010 here.

The Parallel FX Library is also available as a CTP that works with the .NET 3.5 framework. You can download the Parallel FX CTP here.

Don't forget to have a read through the C# documentation as well for more information on the DLR.

 



About the Author

SM Altaf

Mendhak is a web developer and a Microsoft MVP who works with ASP.NET and PHP among the usual array[] of web technologies. He is also rumored to be a three eyed frog, but the evidence is lacking. He can be contacted via his website, www.mendhak.com.


Related Articles

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Managing your company's financials is the backbone of your business and is vital to the long-term health and viability of your company. To continue applying the necessary financial rigor to support rapid growth, the accounting department needs the right tools to most efficiently do their job. Read this white paper to understand the 10 essentials of a complete financial management system and how the right solution can help you keep up with the rapidly changing business world.

  • With 81% of employees using their phones at work, companies have stopped asking: "Is corporate data leaking from personal devices?" and started asking: "How do we effectively prevent corporate data from leaking from personal devices?" The answer has not been simple. ZixOne raises the bar on BYOD security by not allowing email data to reside on the device. In addition, Zix allows employees to maintain complete control of their personal device, therefore satisfying privacy demands of valued employees and the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds