understanding private setters

Logically.

The presence of a private setter is because you can use auto property:

public int MyProperty { get; set; }

What would you do if you want to make it readonly?

public int MyProperty { get; }

Oh crap!! I can’t access it from my own class; I should create it like a normal property:

private int myProperty;
public int MyProperty { get { return myProperty; } }

Hmm… but I lost the “Auto Property” feature…

public int MyProperty { get; private set; }

AHHH.. that is better!!

Leave a Comment