Automatic Properties in C# 3.0 - sweet!
In programming languages size matters - and smaller is better. Automatic properties will certainly reduce boilerplate code, and I wish they were even shorter; but it's better than nothing. An example:
string Name {get; set;}
If you follow common practice and don't declare instance fields public, the only way to achieve such functionality is by writing out a field declaration and a property with explicit getter and setter which return that field and set it, respectively. There are boilerplate code generating plugins for Visual Studio which reduce the tedium somewhat and even visual studio has some functionality for making a property based on a field. Code-generation approaches like that don't appeal to me however, as they only solve the writing part of the problem - and since you and others are liable to need to read that code, it's nice to have a concise, clear syntax.
Of course, in many projects you can just ignore common practice and declare instance fields public and get the same effect, but you might run into issues with reflection based API's, to which fields and properties are fundamentally different.

4 Comments:
But... the syntax you give is the same as the syntax for an abstract property. In C# 2.0, you can write this in an abstract class to indicate: "derived classes must have a property Name with a getter and a setter". So another syntax would be desirable.
That's a good point, but I'm happy that they have anything like this at all - constantly managing boilerplate code is not my idea of fun.
The syntax is not the same because "abstract" keyword is needed.
Well I guess that resolves thomas's objection then.
Post a Comment
Subscribe to Post Comments [Atom]
<< Home