6

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?

hattenn
  • 4,371
  • 9
  • 41
  • 80
  • Similar to: http://stackoverflow.com/questions/3310186/are-there-any-reasons-to-use-private-properties-in-c – matthewr Aug 18 '12 at 00:53
  • If the code you've posted is your complete getter and setter code, I'd suggest you to use automatic property instead. I.e. "public string MyProperty{ get; private set; }" without backing field at all. The backing field will be added automatically by the compiler. – Soonts Aug 18 '12 at 00:57

6 Answers6

7

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:

  • automatically update other fields
  • Validate the new value (eg. ensure an email address matches a regular expression)
  • To keep code that needs to be run every time a field is updated in one place

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;
    }
}
ronalchn
  • 12,225
  • 10
  • 51
  • 61
  • ...although there's not much protection here against fiddling the backing field directly. – spender Aug 18 '12 at 00:57
  • 3
    Generally, you assume that a class can trust itself, since you probably know what you are doing if you are implementing the class. This is as opposed to other programmers using your class, and may not know exactly what they are doing. – ronalchn Aug 18 '12 at 01:07
5

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.

Reinier Torenbeek
  • 16,669
  • 7
  • 46
  • 69
3

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.

Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
2

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;
        }

    }
}
Daniel Figueroa
  • 10,348
  • 5
  • 44
  • 66
1

Property mainly serves for 2 purpose:

  • avoid unexpected access to the underlying field from outside
  • hide the implementation detail of the underlying field, so you can change the implementation freely as long as you keep the interface.

As to a private setting, while the first doesn't make much sense, the second item is still valid

Baiyan Huang
  • 6,463
  • 8
  • 45
  • 72
1

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.

Hassan Boutougha
  • 3,871
  • 1
  • 17
  • 17