September 20, 2007

C# 3.0 Auto Implemented Properties

Part of getting ready for OpenForce '07 involved looking into Visual Studio 2008 and Silverlight.  As happens every time I browse new technology I got quickly sidetracked and sumbled upon a Microsoft document that outlines all of the new features with C# 3.0 which will be released with Visual Studio 2008 as part of the .NET 3.0 framework.  This article will give you a quick overview of the first thing I found, and honestly one of my favorites overall "Automatically Implemented Properties"

I know that everyone is really excited for Silverlight, better WPF support, LINQ and the vast amount of other new features with the upcoming version of Visual Studio.  However, in my testing I found one small modification to the C# specification that I personally find to be one of the most potentially helpful of all.  As many of you know a lot of my recent time has been spent developing DotNetNuke applications and other data driven applications.  One core foundation item that we must implement when working with database access is to build "information" classes.  This can be a very tedious process, especially since Visual Studio doesn't have any nifty "snippets" for C# generation of properties.  This lead me a while back to build my "info class generator" program that you can download from this website.

For those of you unfamiliar with an "info" class by that name, I am simply talking about a class that contains public properties for all columns in a specific table or record set.  Each item will have its own private method to hold the value and a public property with get and/or set methods to manipulate the private variable.  In many cases you are simply directly placing or retreiving the values from the internal variables.  This is very tedious.  Below is an example of the code needed to create a class with 3 properties using the "current" methods (C# 2.0)

public class MyClass
{
    private int _x;
    private string _myValue1;
    private string _myValue2;
    public int X
    {
        get { return _x; }
        set { _x = value; }
    }
    public string myValue1
    {
        get { return _myValue1; }
        set { _myValue1 = value; }
    }
    public string myValue2
    {
        get { return _myValue2; }
        set { _myValue2 = value; }
    }
}

As you can see this is a fair amount of code to create the properties in the "proper" manner. We have a total of 24 lines of code. With C# 3.0 we can use the new Auto implemented properties functionality to compress it to the following.

public class MyClass
{
    public int X { get; set;}
    public string MyValue1 { get; set;}
    public string MyValue2 { get; set;}
}

This code in the end does the exact same thing however we completed it in 6 lines of code rather than 24.  The compilier will automatically generate the private members of the class for us. 

One thing to note; you must have a get AND a set for each property declared via this method.  Luckly you can use accessibility modifiers to make read only properties.  So to create out above class with the X property as read only we would use the following.

public class MyClass
{
    public int X { get; private set;}
    public string MyValue1 { get; set;}
    public string MyValue2 { get; set;}
}

I hope this has been a helpful overview of things to come with C# 3.0.  Please share your comments below, if there is a demand I will create more articles of this nature in the future.

tags: .NET 3.5, C#
comments powered by Disqus

Content provided in this blog is provided "AS-IS" and the information should be used at your own discretion.  The thoughts and opinions expressed are the personal thoughts of Mitchel Sellers and do not reflect the opinions of his employer.

Content Copyright

Content in this blog is copyright protected.  Re-publishing on other websites is allowed as long as proper credit and backlink to the article is provided.  Any other re-publishing or distribution of this content is prohibited without written permission from Mitchel Sellers.