Back to all posts

C# 3.0 Auto Implemented Properties

Posted on Sep 20, 2007

Posted in category:
Development
C#

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 stumbled 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 recordset. 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 retrieving 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)

.NET 2.0 Class Example
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.

Simplified Class with Auto Properties
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 compiler 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. Luckily you can use access modifiers to make read-only properties. So to create out above class with the X property as read-only we would use the following.

Auto-Properties with Read Only Propert
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.