Property declaration
public int Age {get; set;}
Functionally equivalent to
private int m_age;
public int Age {
get {return m_age;}
set {m_age = value;}
Here, there is little difference between a property and a member variable. Equivalent to public int age; In the beginning, ignoring limitations, read and Write permissions.
First lazy, write
public int Age {get; set;}
The project is written to a stage, requiring the age value to be valid. Not older than 0.
Then switch into
private int m_age;
public int Age {
get {return m_age;}
set {
if (value < 0) {
Throw an anomaly or something ...
}
M_age = value;
}
}
Or at some point, say age > 30 don't get married or anything.
Then switch into
private int m_age;
public int Age {
get {return m_age;}
set {
if (value < 0) {
Throw an anomaly or something ...
}
M_age = value;
if (value > marryed && = = False) {
To trigger an event ...
}
}
}
Or another stage, saying that age cannot be set externally, only birthdays can be set externally. Age is read-only and is calculated by birthdays.
Then switch into
public int Age {
Get {return this year-year of birthday;}
}
After this modification, all of the original code that was directly set to age will not be compiled. You can simply comment out.That is, in the
attribute, we can customize the read and write control of the variable, in the read and write to the internal as far as possible exceptions to the processing out, in reading and writing is relatively safe.
C # custom attributes (different from member variables)