1: code similar to this:
Public class car
{
Private string name;
Public string name
{
Get {return name ;}
Set {name = value ;}
}
...
}
To use automatic attributes:
Public class car
{
Public string name {Get; Set}
...
}
The benefit of using property here rather than just using public member variables is:
(1): maintains the encapsulation principle and does not expose public variables to the outside world;
(2): it is easy to add control logic in the future. Or you can close a read or write direction in the get or set;
(3): When metadata is used, the compiler only recognizes attributes but does not recognize member variables. For example, parsechildren (true, "membername") (I think it is online. Actually, I don't understand what it means... Columns here for the moment );
(4): attributes are of special use when binding data (I don't know much about this ...).
2: The default value cannot be specified for the automatic attribute. To achieve this effect, you can only initialize it in the constructor!
In addition, to specify automatic attributes, you must specify get; set. The compilation fails.
Public string name {Get; set,
Public string name {Get;} or public string name {set;} is unavailable!
What should I do if I want to automate attributes and read-only or write data?
Write as follows:
Public string name {Get; private set ;}
Public string name {private get; set ;}