Wednesday, August 22, 2007
An upcoming new feature of C# 3.0 and part of Visual Studio 2008 ( Orcas ) will be Automatic Properties. This feature is meant to help reduce the number of lines of code a developer has to make and improve the developer experience. Currently, when a developer creates a new class, he has to create a getter and setter for each field. This seems rather redundant when you have no initial setup logic to add, but there are very good reasons such as data binding and future assembly compatibility.  I'm sure most developers have written code similar to the following before:
 
   1: class Customer
   2: {
   3:     private string _FirstName;
   4:     private string _LastName;
   5:  
   6:     public string FirstName
   7:     {
   8:         get
   9:         {
  10:             return _FirstName;
  11:         }
  12:         set
  13:         {
  14:             _FirstName = value;
  15:         }
  16:     }
  17:     public string LastName
  18:     {
  19:         get
  20:         {
  21:             return _LastName;
  22:         }
  23:         set
  24:         {
  25:             _LastName = value;
  26:         }
  27:     }
  28: }

A pretty straight forward customer class with two properties ( FirstName, LastName ).  Now with automatic properties, the same code can be compressed down to fewer lines. When the compiler encounters code like the following, it will automate the generation of the private fields along with get and set method.  This lets the developer write fewer lines of code up front and follow good class design. Later on, if needed, the developer can put custom getter/setter code without having to recompile the consuming assembly.

   1: class Customer
   2: {
   3:     public string FirstName { get; set; }
   4:     public string LastName { get; set; }
   5: }

The last information I've heard is that this as a C# only feature. I haven't heard of anything similar for VB.NET at this time. 

posted on Wednesday, August 22, 2007 1:11:03 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0]
Related posts:
VS 2008 & .NET 3.5 SP1 Beta
Free Software for Students
Heroes Happen Here Comic
.NET Graphical Resources
Create an Access Database in C#
Organize Usings