in Development

C# 3.0 – Automatic Properties

Properties are widely used in .NET applications, but it’s also very common to declare their get/set accessors without any business logic, just with the minimum code to return and set the value of the property. Now it’s possible to reduce the minimum code you need to type in order to have a property up and running.

Before automatic properties we needed to write:

private int count;
public int Count
{
    get { return count; }
    set { count = value; }
}

However, now you can do it as follows:

public int Count { get; set; }

The compiler will be in charge of declaring the private field that will be used as storage for the property value and the necessary methods to access/set the value.

I don’t see too many benefits in this feature from a developer perspective, with the “prop” code snippet you reduce even more the amount of code you need to type, in addition I really don’t like the syntax. I know it’s different but in my opinion it’s too similar to abstract properties or properties declared in interfaces. In fact, if you don’t add both, set and get, accessors you will get the error “must declare a body because it is not marked abstract or extern. Automatically implemented properties must define both get and set accessors”. 

I understand that introducing new keywords in a language is always a pain for backwards compatibility, but in this case I would seriously consider adding something like “property”, in that way we could declare automatic properties like this:

public property int Count;

I think something similar is clear and reflects very good what it is …