In C#, we can do something like:
private string _myField;
public string MyProperty
{
get { return _myField; }
private set { _myField = value; }
}
What is the advantage of using a private setter in the property here while we can set _myField inside the class as we want? Why would we want to use the setter of MyProperty?
A setter can implement other behaviour/logic when a property is updated, so that you don’t have to manually implement it at every location where a property might be updated.
It can:
For example: