public class Person {
private string _firstName;
private string _lastName;
private int _age;
public string FirstName {
get {
return _firstName;
}
set {
_firstName = value;
}
}
public string LastName {
get {
return _lastName;
}
set {
_lastName = value;
}
}
public int Age {
get {
return _age;
}
set {
_age = value;
}
}
}
private string _firstName;
private string _lastName;
private int _age;
public string FirstName {
get {
return _firstName;
}
set {
_firstName = value;
}
}
public string LastName {
get {
return _lastName;
}
set {
_lastName = value;
}
}
public int Age {
get {
return _age;
}
set {
_age = value;
}
}
}
Scott Guthrie 在博客中这样诠释:
这是因为,向外面呈现公开的成员变量有很多不好的地方。二个最大的问题是:
1) 你无法轻易地对成员变量做数据绑定,
2) 如果你从类中向外呈现成员变量的话,之后,你不重新编译那些引用老的类的任何程序集,就无法将它们改成属性(譬如,要添加验证逻辑到setter里)。