private readonly PlayerCollection players = new PlayerCollection();
public PlayerCollection Players { get { return players; } }
or
public PlayerCollection Players { get; private set; }
public MyClass()
{
Players = new PlayerCollection();
}
Which would be preferable? Is there a context where one the of two ways is best suited?
Well the first way has the advantage that the field is
readonly, so you can’t set it again elsewhere in the class; the second way has the advantage that it isn’t, so you can. To my mind, that’s a good way to decide between them – do you want the field to bereadonly? If so, first way; if not, second way.