【发布时间】:2009-03-21 10:03:06
【问题描述】:
集合类型属性是否需要设置器
//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; }
}
}
什么时候使用 Type 1 和 Type 2 ?
如果我初始化一个 List 并使用只读属性 Customers 还不够吗?如Company.Customers.Add(new Customer)
为集合类型属性提供设置器的最佳做法是什么?
【问题讨论】:
标签: c# oop collections