C # in Get and set, it seems to see or not understand, popular point to explain, with what is the advantage, not how
If you write like this, there's nothing different.
private int __old;
public int old{
Get{return __old;}
Set{__old = value;}
}
But if you write like this, it's different.
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.
Defining a property in a class
public class Student
{
private string Name
public string Name
{
Set{name=value;} Here is the assignment to the private property name
Get{return name;} The value of the private property name is taken out here
}
}
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.
The difference between a property and a normal variable is that the normal variable is the thing that is placed in the room, what is plainly. And the property is to put a gatekeeper at the door of the house, you put things to go through him.
The gatekeeper is a property accessor, and the thing is get, and the thing is set. It's up to you how you put it, though you feel like you're just taking it directly.
Get set is an external interface, generally accessing member data in the instance requires this member data access is public, now C # is very advanced, get is GET, set is set, but it provides a good shrinkage, the get and set can be set to separate access permissions For example, you can only read or write to a member's data, or only derived classes may access ... Rather than previously, there is no property accessor, which requires a function to invoke the private member data, which provides an efficient access pattern and simple writing.
It 's for security.
The field is the data you specifically want to manipulate so his value can not be wrong, but you can not avoid users make mistakes ~ ~
Use attributes to assign a value to a field for security, because you can verify the security of the data in set.
Get is much simpler to validate the data and give the value to the field so get
The Chinese meaning of set is "setting";
The Chinese meaning of get is "get";
Add:
Get and set are automatically called at the appropriate time
The main purpose is to hide the data structure in the program.
Get is the value that gets the property,
Set sets the value of this property.
There is a special variable value when using Set
Is the value when setting the property
The value implicit parameter, which sets the accessor and adds or removes an event handler.
To set the properties of a class
For example, use the class name. Property = "" Assignment Set function
variable = class name. property value Get function
C # GET and set functions