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.

Posted by Mitchel on Thursday, September 20, 2007
 

Comments

Cool. That feature seems like it should have been added ages ago.

By Michael on Thursday, October 04, 2007 at 10:15 PM

Great but I have better than that; and it works even using in .NET 1.0:

public class MyClass
{
public readonly int X;
public string MyValue1;
public string MyValue2;
}

Can you tell the difference ?

By Zolof on Wednesday, December 12, 2007 at 10:34 PM

Zolof,

Yes, your method is similar but a public data element can have it's downfalls, and honestly it isn't the recommended practices for Object Oriented Design.. Granting public access to a property is considered a bad practice by most...

By mitchel.sellers@gmail.com on Thursday, December 13, 2007 at 3:45 AM

Comments from the following blog entry: C# 3.0 Auto Implemented Properties, located at: http://www.dotnetkicks.com/csharp/C_3_0_Auto_Implemented_Properties

By DotNetKicks.com on Monday, March 17, 2008 at 12:36 PM

Thanks for sharing.
I was really wondering what happened to the snippet 'prob'...

By Miron on Wednesday, March 19, 2008 at 10:07 AM

Zolof: your comment re just using Public variables really shows a lack of OO knowledge. Readonly is not the same as a private set as you can only set the value once with readonly but private set allows settings it from within the class as required. The only water your method holds is for a get and set which are both public, but if you code using Interfaces, which you should, then your method falls apart again ;-) Using public variables is, as mentioned, really bad.

By SuperWasabiDave on Friday, May 09, 2008 at 12:47 AM

Example is fine. But it shows a warning. Say I have a private field called val. and I am writing property (autocomplete) for this. This shows a warning as val was never used

By Abhay Kumar Pandey on Tuesday, November 18, 2008 at 6:09 PM

Abhay

That is the point of auto implemented properties, you do NOT have to implement the private member!

By mitchel.sellers@gmail.com on Wednesday, November 19, 2008 at 3:34 AM

I was recently wonderin if the auto-implemented prosperties let for smth like beneth done with 'old concept' of prosperties:

class Data
{
private static double _inch = 2.54;
public static double inch
{
get { return _inch;}
set { _inch = value;}
}

}

Namely assigning a value what is in my opinion impossible with use of those auto-implemented prosperties

By Peter on Friday, November 21, 2008 at 10:45 AM

That is the beauty here, it happens behind the scenes...

By mitchel.sellers@gmail.com on Friday, November 21, 2008 at 11:51 AM
Click here to post a comment

Disclaimer