setter方法收集类型属性 [英] Setters for collection type properties

查看:171
本文介绍了setter方法收集类型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于集合类型属性必须制定者

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比2型?
岂不是不够的,如果我初始化一个名单,放大器;使用只读属性的客户?在 Company.Customers.Add(新客户)

什么是相对于最佳实践提供setter方法集合类型属性?

What is the best practice with respect to providing setters for collection Type properties?

推荐答案

请阅读的FxCop recommondation 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 contains good advice :)

这篇关于setter方法收集类型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆