Tuesday, May 13, 2008

Auto Implemented Properties (DotNet v3)

I just came across the option of using something called "Auto Implemented Properties" which is new to DotNet version 3.0.

The nice thing about this feature is that not only saves a lot of code in the simple scenarion where you only have a "dumb" property passing and returning a private field value in and out. But it is more conceptally right.

For example: public string Name { get; set; } will return a "true" property.

It is conceptually more right becauseit makes sure nobody access your fields instead of your properties like it should for an avarage application which makes it more readable and more maintainable. And there is less type redundancy aswell. Thus in my opinion this is more conceptually and practically right!

Just for the record: in many cases it is obvious (for sake of Anticipation Af Change) that you want to use a property and not just a field.

An example to fall in love with:

public class Contact
{
public string Name { get; set; }
public string Address { get; set; }
public int ContactNumber { get; set; }
public int ID { get; private set; } // readonly
}

No comments: