Automatically Implemented Properties - Visual Studio Orcas C# Compiler - C# 3.0
by David Hayden ( Florida MVP C# ), Filed: C# 3.0 Tutorials
Last night I was flipping through the powerpoint slides of Anders Hejlsberg - C# 3.0: Future Directions in Language Innovation, when I came across a little blurb on one of the sides that mentioned the new Automatic Properties feature that is part of the C# compiler in Visual Studio Orcas. With Automatically Implemented Properties, one can now write this in a C# Class:
public string Name { get; set; }
and it will essentially function as if you had typed this:
private string name;
public string Name
{
get { return this.name; }
set { this.name = value; }
}
Again, this is not a C# 3.0 enhancement, but done via the compiler.
I say it will essentially work as above, because it sounds like one will not have access to a private member since you aren't declaring one. All interaction will happen with the property itself, Name in this case.
In the video where Peter Hallam and Charlie Calvert talk about this new feature, Peter implies that customers really wanted this feature because they were often using public fields because it was too labor intensive to write the longer property declaration mentioned above.
When I hear this a couple of things come to mind:
-
Should this enhancement be done in the compiler or perhaps in the IDE?
-
Haven't those customers heard of CodeRush, ReSharper, etc?
Now the feature is cool and I welcome all kinds of productivity enhancements, but this feels like one of those features that would have been cooler if we didn't have all these code generation tools, like CodeRush, ReSharper, CodeSmith, etc.
I, personally, don't write property declarations often anymore. In both CodeRush and ReSharper, I just type "ps" and I get a live template that asks me for the name of the string property and completes the declaration to my specification and using my preferred coding style. In other cases, I use CodeSmith to generate numerous classes ( and property declarations ) when I can take advantage of bulk code generation.
Then I thought, well..., it does help with readability, but then I usually toss all the private and property members between regions anyway to keep them from cluttering my view.
#region Properties
private string name;
public string Name
{
get { return this.name; }
set { this.name = value; }
}
#endregion
Most of the time I just see this in my IDE anyway :)

Because I get this type of productivity in the IDE today with CodeRush and ReSharper, I wonder if keystroke saving enhancements ( as implied in the video ) should be done in the IDE as opposed to the compiler. Now I realize at the compiler level they will probably be doing some code optimization / inlining ( I hope ), but this still feels like more of an IDE enhancement as opposed to a compiler enhancement.
Still... the feature is cool and I appreciate the productivity enhancement! I just would have been more excited a few years ago.
by David Hayden ( Florida MVP C# )
Filed: C# 3.0 Tutorials