posted on Monday, March 12, 2007 6:11 PM by Obi

Orcas comes with goodies

Orcas which is slated to be released sometime this year comes with a plethora of new language features and improvements for both C# and VB developers. The CTP version of Orcas was shipped just recently.  Language features like Automatic Properties, Object Initializers, Collection Initializers just to name a few will likely enhance the development experience and increase productivity and take less time to type the code.

New C# Language Feature: Automatic Properties

As a C# developer today, you are probably quite used to writing classes with basic properties like the code-snippet below:

public class Person {

        
private string _firstName;
        private string 
_lastName;
        private int 
_age;
        
        public string 
FirstName {

            
get {
                
return _firstName;
            
}
            
set {
                _firstName 
= value;
            
}
        }

        
public string LastName {

            
get {
                
return _lastName;
            
}
            
set {
                _lastName 
= value;
            
}
        }        
        
        
public int Age {

            
get {
                
return _age;
            
}
            
set {
                _age 
= value;
            
}
        }
    }

Note about that.. we aren't actually adding any logic in the getters/setters of our properties - instead we just get/set the value directly to a field.  This begs the question - then why not just use fields instead of properties?  Well - there are a lot of downsides to exposing public fields. Two of the big problems are:

  1. You can't easily databind against fields.
  2. If you expose public fields from your classes you can't later change them to properties (for example: to add validation logic to the setters) without recompiling any assemblies compiled against the old class. 

For example, using automatic properties, the above code can now be re-writen as below:

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

        
public string LastName {
            
get; set;
        
}        
        
        
public int Age {
            
get; set;
        
}
    }

 

Or to make it even more concise, you can write as follows:

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

 

New C# and VB Language Feature: Object Initializers

Types within the .NET Framework rely heavily on the use of properties.  When instantiating and using new classes, it is very common to write code like below:

   Person person = new Person();
   
person.FirstName "Obi";
   
person.LastName "Oberoi";
   
person.Age 35;

Have you ever wanted to make this more concise (and maybe fit on one line)?  With the C# and VB "Orcas" compilers you can now take advantage of a great "syntactic sugar" language feature called "object Initializers" that allows you to-do this and re-write the above code like so:

  Person person = new Person { FirstName="Obi", LastName="Oberoi", Age=35 };

The compiler will then automatically generate the appropriate property setter code that preserves the same semantic meaning as the previous (more verbose) code sample above.

In addition to setting simple property values when initializing a type, the object initializer feature allows us to optionally set more complex nested property types.  For example, assume each Person type we defined above also has a property called "Address" of type "Address".  We could then write the below code to create a new "Person" object and set its properties like so:

   Person person = new Person {
      FirstName 
"Obi",
      LastName 
"Oberoi"
      
Age 35,
      Address 
= new Address {
         Street 
"25 Kingsbridge Garden Circle",
         City 
"Mississauga",
         Province
"Ontario",
         PostalCode 
"L5R4B1"
      
}
   }
;

New C# and VB Language Feature: Collection Initializers

Object Initializers are great, and make it much easier to concisely add objects to collections.  For example, if you wanted to add three people to a generics-based List collection of type "Person", you could write the below code:

  List<Person> people = new List<Person>();
            
  
people.Add( new Person { FirstName "Obi", LastName "Oberoi", Age 35 } );
  
people.Add( new Person { FirstName "Sia", LastName "Oberoi", Age } );
  
people.Add( new Person { FirstName "Sunita", LastName "Oberoi", Age 35 } );
 

Using the new Object Initializer feature alone saved 12 extra lines of code with this sample versus what you'd need to type with the C# 2.0 compiler.

The C# and VB "Orcas" compilers allow us to go even further, though, and also now support "collection initializers" that allow us to avoid having multiple Add statements, and save even further keystrokes:

   List<Person> people = new List<Person> {
      
new Person { FirstName "Obi", LastName "Oberoi", Age 35 },
      
new Person { FirstName "Sia", LastName "Oberoi", Age },
      
new Person { FirstName "Sunita", LastName "Oberoi", Age 35 }
   }
;

When the compiler encounters the above syntax, it will automatically generate the collection insert code like the previous sample for us.

Cheers,

Obi Oberoi

 

Comments