集合类型属性的集合 [英] Setters for collection type properties
本文介绍了集合类型属性的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是集合类型属性必需的设置器
Are setters necessary for Collection Type Properties
//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&使用readonly属性客户?如 Company.Customers.Add(new Customer)
When do I use Type 1 vs Type 2 ?
Wouldn't it suffice if I initialize a List & use readonly property Customers ? as in Company.Customers.Add(new Customer)
有关提供setters的最佳做法是什么集合类型属性?
What is the best practice with respect to providing setters for collection Type properties?
推荐答案
请阅读FxCop的推荐CAS2227集合属性应该是只读的
http://msdn.microsoft.com/en-us/library/ms182327(VS。 80).aspx
Please read the FxCop recommondation CAS2227 "Collection properties should be read only" http://msdn.microsoft.com/en-us/library/ms182327(VS.80).aspx
它包含良好的建议:)
这篇关于集合类型属性的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文