John Liu .NET

View Original

Having different scope for your property

C# 2.0 gave us this:

private string _property;
public string Property
{
    get { return _property; }
    private set { _property = value; }
}

C# 3.0 gave us this:

public string Property
{
    get;
    private set;
}

Due to the rules of encapsulation, the accessor/getter usually has a more relaxed scope than the mutator/setter.

I've recently came across cases where the tables are turned.  Hurray for some dependency-injectioness!

[CreateNew]
IService MyService
{
   private get;
   internal set;
}