What is the difference between property with a private setter and property without a setter?

If I want a read-only property, I write it like this:

public int MyProperty { get { //Code goes here } } 

However, the Microsoft example (and several other examples I saw) are written like this:

 public int MyProperty { get; private set; } 

Is there a difference between the two, and do I need to start writing such properties?

+4
source share
5 answers

As you can see in the second example, you can leave the implementation for the property. Then, .NET will automatically create a local variable for the property and implement simple retrieval and configuration.

 public int MyProperty { get; private set; } 

virtually equivalent

 private int _myProperty; public int MyProperty { get { return _myProperty; } private set { _myProperty = value; } } 

Record

 public int MyProperty { get; } 

does not work at all, since the automatic properties must be implemented by the receiver and setter, and

 public int MyProperty { get; private set; } 

provides you with a property that can return any int , but can only be changed in the current class.

 public int MyProperty { get { ... } } 

Creates a read-only property.

Question: what do you need? If you already have a member variable that is used in your class, and you only want to return the current value using the property, you will do fine with

 public int MyProperty { get { return ...; }} 

However, if you want a read-only property that needs to be set inside your code (but not from other classes) without explicitly declaring a member variable, you need to go with the private set approach.

+8
source

Using a private setter, you can only assign a property value inside an instance; if the property does not have a setter, you cannot set its value anywhere.

+5
source

If you are not using explicit member assignment in a property, you will need to declare a private set , at least to set the value for this property. Otherwise, during compilation, you will receive a warning that your property cannot be assigned.

If you use an explicit member, you can directly assign this value to this element without adding a private set :

 private int member ; public int MyProperty { get { return member; } } // ... member = 2; int anotherVariable = MyProperty; // anotherVariable == 2 
+4
source
 public int MyProperty { get { // Your own logic, like lazy loading return _myProperty ?? (_myProperty = GetMyProperty()); } } 

A property with only the recipient is very useful if you need your own logic for accessing this property, in particular when you need the property to be lazy.

 public int MyProperty { get; private set; } 

A property with a private setter is useful if you need a property that cannot be changed externally but is still supported inside the class.

In both cases, you may have a backup data field for the actual value, but in the first you will have to maintain it yourself, and in the second - for you the generated code.

+2
source

There is a difference when you access an object with reflection.

 public class Foo { public string Bar { get; private set; } } // ..... internal static void Main() { Foo foo = new Foo(); foo.GetType().GetProperty("Bar").SetValue(foo, "private?", null); Console.WriteLine(foo.Bar); } 
+1
source

Source: https://habr.com/ru/post/1485940/


All Articles