Reputation: 4399
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
?
Upvotes: 6
Views: 727
Reputation: 3929
need a private setter in a property is to use your field wrapped in property only for being modify directly by other function, properties of your class. So in one place (property) you set value of your field but all other elements of your class have not to access directly to your private field but through property that wrapped it.
Upvotes: 1
Reputation: 12335
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:
private string _myField;
private int _myField_num_updated;
private DateTime _myField_updated_at;
public string MyProperty
{
get { return _myField; }
private set {
_myField = value;
_myField_num_updated++;
_myField_updated_at = DateTime.Now;
}
}
Upvotes: 7
Reputation: 10666
With a setter you can control the value when you try to set it, for example if you have an int that you want to be sure does not have a value greater than ten.
public int MyProp
{
get { return _my_prop;}
private set {
if value > 10 {
_my_prop = 10;
}
}
}
Upvotes: 2
Reputation: 6771
Property mainly serves for 2 purpose:
As to a private setting, while the first doesn't make much sense, the second item is still valid
Upvotes: 1
Reputation: 17373
The goal of using property accessors (get
and set
) is to hide the internal implementation on how particular values are queried or modified. In your case, the setter is simple, but in the future, it might become more complicated. By hiding the implementation, you minimize the impact of potential changes and keep the interface simple.
Now concerning your question: why using a private setter? The reason for using a private setter is exactly the same as the reason for using a setter in general. In the case of a private setter, the user of that setter is the code of the class itself. In case of a public setter, the user could be anybody using the class. The advantage of using the setter remains the same.
Upvotes: 5
Reputation: 185593
Because while properties more often than not are wrapping backing fields, there is no reason that they must. Properties can also implement custom business logic (usually some kind of validation or conversion) and call other functions. Using a private setter makes your code more idiomatic (meaning others will more readily understand what you're doing) and more expressive (meaning the code will, or should, more closely reflect the semantics of what you are trying to accomplish. Private setters also allow this custom logic to be encapsulated, so that there's only one place that you need to change it in the event that logic changes or the code needs refactoring.
Upvotes: 3