我创建的两个属性之间有什么区别(即优势/劣势)?两者似乎都是正确的,但是在类中声明属性的最佳方式(实践)是什么?属性
[Serializable]
public class MySample
{
public string String1 = string.Empty;
private string _string2 = string.Empty;
public string String2
{
get { return _string2 ; }
set { _string2 = value; }
}
}
可能的重复http://stackoverflow.com/questions/450238/to-underscore-or-to-not-to-underscore-that-is-the-question – coder 2013-02-20 15:22:32
如果你想,例如,举一个事件当财产发生变化时。由于您只能在set/get访问器中放置代码,因此您必须使用“String2”选项。 – Evelie 2013-02-20 15:24:49
[Property(with no extra processing)vs public field](http://stackoverflow.com/questions/1272521/propertywith-no-extra-processing-vs-public-field) – nawfal 2014-07-17 20:37:21