Sunday, July 12, 2009

Asymmetric Accessor Accessibility and Automatic Properties

(Cross post from IRefactor)

Asymmetric Accessor Accessibility – is a feature that was introduced in .NET 2.0 in order to allow different accessibility levels to get and set portions of a property or an indexer. Those get and set portions are called accessors.

In the example below, the get accessor is public, whereas the set accessor is restricted and private.

private string id;
public string Id
get { return id; }
private set { id = value; }

Automatic Properties – is a syntactic sugar feature, introduced in .NET 3.5 to allow more concise property declaration.

public string Id {get; set;}

The above features, can be combined together to form a very elegant and concise property declaration, with asymmetric accessibility levels:

public string Id {get; private set;}

Using “automatic asymmetric accessibility properties” contributes to the clarity, elegance and correctness of the code.

One of such examples is immutable class implementation.

No comments:

Post a Comment