.NN #15: Not All VS 2008 Code Features Are 3.5 Specific
So your company decided to roll out Visual Studio 2008, but you have only .NET 2.0 based applications. This is perfectly fine because of the great new Multi-targeting feature in VS 2008 that lets you target the 2.0, 3.0 or 3.5 Frameworks for each project; however, the company directive has been that you’ll be updating the applications to 3.5 on an as needed basis, or only when there are changes to the project. Oh, and you’re not in the group that is working on the change overs, so you’re out of luck … or are you?
Not all the code enhancements that come in Visual Studio 2008 are framework based, in fact several are simply compiler based. The same compiler is used by VS 2008 no matter what the targeted framework, so this means that we can take advantage of a few of the code based features with our .NET 2.0 targeted projects!
NOTE: This post is not meant to teach you how to add the new features (check out the links I have to other posts for that), only point out which ones you can use with a .NET 2.0 targeted project.
Note that I targeted the .NET Framework 2.0 in the drop down on the New Project dialog when I created my test project the code below is from.
First let’s start with Auto-implemented properties:
public class Foo
{
public string Bar { get; set; }
}
This short hand coding style is a compiler based feature, not something built into 3.5. It doesn’t save you a ton of time (but does save typing), but it’s concise. The syntax above is equivalent to:
private string _bar;
public string Bar
{
get { return _bar; }
set { _bar = value; }
}
In fact, if you type these two above and compile them separately and bring them up in Reflector, you’ll see that indeed they are compiled to the exact same thing, it’s just that the backing store (_bar in the second example) get an odd name that the compiler generates.
Next we have Implicitly Typed Local Variables:
public Int32 Bar(Int32 test)
{
var x = 10;
return test + x;
}
The example above doesn’t really give you much at all, but what is going on here is that the compiler is looking at the assignment of the variable and knows that x must be an Integer (defaults to Int32). The above code also compiles directly as if you had declared x an Int32 to begin with.
Next up, Anonymous Types:
var myBar = new { adjective ="lead", noun="balloon" };
Console.WriteLine("This is going over like a {0} {1}.", myBar.adjective, myBar.noun);
With anonymous types you’re able to create an object instance on the fly without actually defining the Type yourself in your code. Note that I’m assigning the myBar variable (see the implicitly typed variable use?) to a new instance of some object that has two string properties: adjective and noun. Then I can make reference to them further down as you see I’m writing these out to the console. What’s going on here is in the background the Type is actually being created for you on the fly and when the compiler boils this down a Type is actually there in your compiled assembly.
Object Initializers are next:
public class Foo
{
public class simpleClass
{
public simpleClass()
{
}
public string Name { get; set; }
public string address { get; set; }
public string city { get; set; }
public string state { get; set; }
public string zip { get; set; }
}
public void Bar()
{
simpleClass example = new simpleClass { Name = "Bob Smith", address = "123 AnyStreet" };
Console.WriteLine(example.Name);
}
}
In the code above notice that the simpleClass Type doesn’t have a constructor that let’s you fill in the Name and Address only, but with object initializers that’s not a problem. The syntax above would have been the same as creating an instance of the simpleClass and then setting the Name and Address lines separately. There are also collection initializers so that you can create collections on the fly like this as well.
Some of you may be asking, what about VB.NET? Well, the compiler enhancements in VB 9.0 are also available for .NET 2.0 projects. The following are available for VB.Net:
- Implicitly Typed Variables via the Dim keyword.
- Anonymous Types
- Object Initializers
I’m sure there are more, but the point of this post was to let you know that not all new features of VS 2008 are available only in 3.5. Any of the compiler based features are there to be used with any version of .NET you can target using VS 2008.
I do want to rant a little here around the concept of compatibility between 2008 and 2005. Several times I’ve heard the question “Can I upgrade to VS 2008 and not affect my 2005 projects?". Several people have answered yes and pointed to the new multi-targeting feature. That’s only partially true. Yes, you can open a .NET 2.0 framework project created in VS 2005 in VS 2008. The first thing it will ask you to do is migrate the solution file to 2008. This is a one way trip. If you have multiple people on your team then all of you will need to upgrade to VS 2008, though your projects will still be targeting the .NET 2.0 framework. Technically you could have a mixed group of developers, but then the VS 2008 developers would need to have their own solution files. Oh, and if they used any of the compiler features listed above the VS 2005 developers will no longer be able to compile, even though the .NET framework version is the same.