Friday, November 23, 2007

Visual Studio 2008 - Automatic Properties in .NET 2.0

In the next couple of blogs we will present shortly some cool and interesting stuff we found about Visual Studio 2008 and .NET Framework 3.5. For all cool stuff see Visual Studio 2008 Official page.
One of the first items we noticed and played around are Automatic Properties. What does it mean? Untill now it was common to define a class as
public class Test
{
public Test() { }

private string _firstName;
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
}
where for each property you usually had a private member variable.
But now, Visual Studio 2008 and .NET framework 3.5 introduces Automatic Proeprties where your class can be defined as

public class Test
{
public Test() { }
public string FirstName { get; set; }
}
and you will notice that a private member variable is missing and a whole body of the property implementation itself (it has just a stub definition get; set; )
Imagine how much you reduce a time for class scripting. Readability is also much better.
But you will probably ask, where does .NET keep your data now? If you take a look at IL you will find that actually a compiler generates a field (variable) for you:
'<>k__AutomaticallyGeneratedPropertyField0'. Whole construction is done just by using CompilerGeneratedAttribute. But, what we had in mind, this attribute is part of .NET Framework 2.0, what lead us to... Hey, Automatic Properties are possible in .NET 2.0 than?! And this is really true. Open your Visual Studio 2008 (Express), create such a construction, as a Target Framework for a project choose .NET Framework 2.0 and you will see it runs. WOW!!! We like this very much and will use it in DBTyP.NET as soon as possible.

No comments: