【发布时间】:2016-05-10 07:56:56
【问题描述】:
在 Josh Smith 的 this 教程中,字段被定义为只读:
public class CustomerRepository
{
readonly List<Customer> _customers;
...
public CustomerRepository(string customerDataFile)
{
_customers = LoadCustomers(customerDataFile);
}
...
}
稍后更新只读列表_customers:
public void AddCustomer(Customer customer)
{
if (customer == null)
throw new ArgumentNullException("customer");
if (!_customers.Contains(customer))
{
_customers.Add(customer);
if (this.CustomerAdded != null)
this.CustomerAdded(this, new CustomerAddedEventArgs(customer));
}
}
这是如何被允许的,使用只读有什么意义?
【问题讨论】: