Automatic Properties in Visual Studio 2008
Posted by: Valer BOCAN in Visual Studio (3,369 Views)After downloading my copy of Visual Studio 2008 Beta 2, I eagerly began to explore its new features. There is a wealth of new things to discover in what’s formerly known as Orcas and I will blog about these as I progress. The first and the most striking evidence is that Visual Studio 2008 Beta 2 is extremely fast as compared to version 2005 running on the same machine. I am very pleased with this and I can almost say that I don’t miss anymore the speed of the Pascal compilers of yesteryear.
Automatic Properties is a nice addition to the already complicated C# syntax. The idea behind automatic properties is to reduce as much as possible the amount of code for commonly employed tasks. To illustrate this, let’s suppose we need a class to hold information about an employee. We could accomplish that by declaring two member variables, as below:

By itself, this class is of no particular use, since the members are declared private and they cannot be accessed from the outside. We need to declare properties (accessors) for each member. Now, we can do this in two ways: either we write the property declaration manually or we refactor the code and use the Encapsulate Field feature, as shown below (right-click the desired variable to get the context menu):

I took the second approach, so the code should now look as follows:

There is nothing special about this class except that it is verbose. Even though we generated most of it using an automated feature, imagine how the code would look for a class that should hold tens of properties for an employee. Such a scenario is not far-fetched so the designers at Microsoft thought of a sort of syntax twist and introduced what is now called Automatic Properties. The code below is equivalent to the previous one:

All we need to declare now are the names of the properties and the compiler takes care of the internal variable and the getter/setter definitions. The syntax of the automatic properties is natural. However, it is yet to see how useful this is going to be in the real world.
My question is: would you use this feature in your projects? Would this feature improve the overall readability of the code or will it worsen it?

Entries (RSS)
October 5th, 2007 at 8:06 pm
I think you have to think of it from a noobs point of view… if you do that, you realize that in general this approach probably decreases the readability of the code. However, for an experienced programmer, it’s pretty obvious what is going on and there is no need to see the extra code. I think I will use this syntax knowing that if anyone comes along in the future and says, “what the heck???” they could probably figure it out pretty quick with a search or two.
February 11th, 2009 at 12:24 am
heck yeah I’ll use it. I’m converting a legacy class to .net It has 64 properties… usual mundane details, first name, last name, address…. yada yada. If I can put each property on a single line like:
public string FirstName {get; set; }
That definitely looks readable enough to me.