C # When defining a class, it is common to encapsulate objects declared in the class so that the property cannot be accessed by the outside world. If you remove the set part of the code above, the outside world can only read the value of name, and if you remove the get part, you can only assign a value to name. This allows you to control the outside access to the private property name, which is a feature of C # .
Of course you can also create a function to value and assign the name, but this is more troublesome.
Attributes are different from normal variables in that they include get and set accessors, which can be used to control access to properties by setting access permissions for the accessors, for example:
private int _old;
public int old{
Get{return _old;}
Set{//added the verification code here
if (value<0)
throw new Argumentoutexception ("value", "input value cannot be less than 0");
_old = value;
}
The above example shows that the properties can be added to the code for processing. You can also set access permissions and so on.
Effects of Get and set properties in C #