C# 3.0 Features: Automatic Properties

.NET 3.5 is out which means all of the great features available in C# 3.0 are available to use now.  Here's a quick list of the main language enhancements available in C# 3.0:

  • Object Initializers
  • Automatic Properties  
  • Anonymous Types
  • Extension Methods
  • Lambda Expressions
  • LINQ
  • Collection Initializers
  • One of the things I can't say that I've enjoyed doing in the past when writing .NET classes was creating properties and associated fields.  I leveraged the "prop" code snippet a lot in Visual Studio 2005 so that I didn't have to type much, but I still had to go through and define the field name and property name in the code.  While public fields could certainly be used in some cases, they don't provide any control over the data being assigned (especially if the field is a string type) and aren't supported well when it comes to data binding.  As a result I always create public properties to wrap fields even if I don't have any "extra" logic that needs added into the get or set blocks.  Here's the standard way of creating fields and properties using C# 2.0:

    public class Person 
    {    
       
    string _FirstName;
       string 
    _LastName;

       public string 
    FirstName 
       {
            
    get return _FirstName}
            
    set { _FirstName = value; }
       }

       
    public string LastName 
       {
            
    get return _LastName}
            
    set { _LastName = value; }
       }
    }

    With C# 3.0 a new feature called "automatic properties" is now available.  When you use the "prop" code snippet in Visual Studio 2008 you'll see that empty get and set blocks are added and that no associated field is added into the code.  The compiler will automatically generate the backing field at compile time if it finds empty get or set blocks saving you the work. You can still add get and set blocks that have additional filtering logic in them as well although you'll have to type all of that yourself of course. 

    Here's an example of how properties can be written in C# 3.0....you gotta love it especially when you compare how much more compact this technique is compared to the older way of doing things.

    public class Person 
    {    
       
    public string FirstName  { get; set; }
       
    public string LastName  { get; set; }

    Published Tuesday, December 4, 2007 8:24 AM by dwahlin
    Filed under: ,

    Comments

    # re: C# 3.0 Features: Automatic Properties

    Wednesday, December 5, 2007 11:19 AM by David

    But, can you still modify the returned value using this method? Say, for example, that I wanted to Trim() the value of the property before returning it. Is this supported with Automatic Properties?

    # re: C# 3.0 Features: Automatic Properties

    Wednesday, December 5, 2007 1:19 PM by dwahlin

    David,

    You can, but you'd have to write the code in the get/set blocks as appropriate and add your own backing field.  Automatic properties are only for situations where no body is in the get or set blocks.

    # re: C# 3.0 Features: Automatic Properties

    Friday, December 28, 2007 9:06 AM by Jeremy Jenkins

    A nice little piece of compiler candy.  I love it.

    # How much should one DataSet represent? - Just just easy answers

    Pingback from  How much should one DataSet represent? - Just just easy answers