These are just accessors and mutators. How properties are implemented in C #
In C # 3, you can use automatically implemented properties, for example:
public int MyProperty { get; set; }
This code is automatically translated by the compiler into code similar to the one you published, with this code it is easier to declare properties, and they are ideal if you do not want to implement your own logic inside set or get , you can use a different access method for the set method, making the property unchanged
public int MyProperty { get; private set; }
In the previous example, MyProperty will be read only outside the class where it was declared, the only way to mutate it is to expose the method for this, or simply through the class constructor. This is useful when you want to control and make explicit the state change of your entity.
If you want to add some logic to the properties, then you need to write properties that manually implement the get and set methods in the same way as you placed:
User logic implementation example
private int myProperty; public int MyProperty { get { return this.myProperty; } set { if(this.myProperty <=5) throw new ArgumentOutOfRangeException("bad user"); this.myProperty = value; } }
Jupaol
source share