Collection Type Properties 是否需要 setter
//Type 1
class Company
{
private IList<Customer> customers;
public IList<Customer> Customers
{
get { return customers; }
set { customers = value; }
}
}
//Type 2
class Company
{
private readonly IList<Customer> customers = new List<Customer>();
public IList<Customer> Customers
{
get { return customers; }
}
}
什么时候使用类型 1 和类型 2?
如果我初始化一个 List 并使用只读属性 Customers 还不够吗?如 Company.Customers.Add(new Customer)
关于为集合类型属性提供 setter 的最佳实践是什么?
最佳答案
请阅读 FxCop 推荐 CAS2227“集合属性应该是只读的” http://msdn.microsoft.com/en-us/library/ms182327(VS.80).aspx
它包含很好的建议:)
关于c# - 集合类型属性的 setter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/669066/