Search
  
 
Mitchel Sellers' DotNetNuke, .NET, and Other Topics BlogMinimize
 
 
Subscribe To Blog Updates by E-Mail

Current Articles | Categories | Search | Syndication

   
 C# 3.0 Auto Implemented Properties
By Mitchel Sellers on Thursday, September 20, 2007 @ 8:47 AM
 
  899 Views :: 6 Comments :: :: .NET 3.5, 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 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.

 
  
 Share/Save This Article 
 

Use the below controls to save this article to one of many popular social bookmarking sites!

   kick it on DotNetKicks.com  
 
  
 Article Rating 
  
  
 Article Comments 
 
By Michael @ Thursday, October 04, 2007 10:15 PM
Cool. That feature seems like it should have been added ages ago.

By Zolof @ Wednesday, December 12, 2007 10:34 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 Mitchel Sellers @ Thursday, December 13, 2007 3:45 AM
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 DotNetKicks.com @ Monday, March 17, 2008 12:36 PM
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 Miron @ Wednesday, March 19, 2008 10:07 AM
Thanks for sharing.
I was really wondering what happened to the snippet 'prob'...

By SuperWasabiDave @ Friday, May 09, 2008 12:47 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.

Click here to post a comment
 

 Add to Technorati Favorites
 Print   
 
  
 
ArchiveMinimize
 
 
 Print   
 
  
 
CategoriesMinimize
 
 
 Print   
 
  
 
DonateMinimize
 
 

Show your appreciation for the content/modules made available by MitchelSellers.com by making a donation. Donations are used to assist with dedicating time to creating free content. 

 Print