Saturday, 6 February 2016

private set

I noticed in passing that you don't need to use a private setter in a C# auto property if you assign it in the constructor.
class Example
{
  public int Foo { get; }
  
  Example(int foo)
  {
    Foo = foo;
  }
}
This is obviously analagous to readonly fields. It's a subtle feature, letting you clearly indicate immutable values in a way that private set doesn't.