这两者之间:为什么我应该使用自动实现的属性而不是字段?
财产:
class WithProperty
{
public string MyString {get; set;}
}
与现场:
class WithField
{
public string MyString;
}
显然我应该选择第一个。为什么?
我听说过这里的意思是允许更改接口,但是如果我有第二个接口并且将其更改为第一个接口,则将不得不更改。重新编译时,一切都将指向 属性。
我在这里错过了一些重要的东西吗?
可能涉及:http://stackoverflow.com/questions/863182/changing-fields-to -property-is-a-breaking-change-under-what-scenarios – mmcdole 2010-07-07 20:16:44
Jon Skeet在这个问题上有一篇很好的文章:http://csharpindepth.com/Articles/Chapter8/PropertiesMatter.aspx – Odrade 2010-07-07 20:20:14