這兩者之間:爲什麼我應該使用自動實現的屬性而不是字段?
財產:
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