为什么它认为不好的揭露列表< T&GT ;? [英] Why is it considered bad to expose List<T>?

查看:90
本文介绍了为什么它认为不好的揭露列表< T&GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据FxCop的,名单不应该在一个API对象模型公开。这是为什么做不好?

According to FXCop, List should not be exposed in an API object model. Why is this considered bad practice?

推荐答案

我同意驼鹿式的非丛林这里:列表< T> 是一个不受约束,臃肿的对象,在它有很多包袱的。

I agree with moose-in-the-jungle here: List<T> is an unconstrained, bloated object that has a lot of "baggage" in it.

幸运的是,解决办法很简单:公开的IList&LT; T&GT; 而不是

Fortunately the solution is simple: expose IList<T> instead.

据公开了拥有最全的名单℃的准系统接口; T&GT;的方法(有东西像的AddRange除外() ),它并不限制你具体列表&LT; T&GT; 类型,它可以让您的API消费者使用<$自己定制的实施者C $ C>的IList&LT; T&GT;

It exposes a barebones interface that has most all of List<T>'s methods (with the exception of things like AddRange()) and it doesn't constrain you to the specific List<T> type, which allows your API consumers to use their own custom implementers of IList<T>.

有关更大的灵活性,可以考虑暴露一些集合到的IEnumerable&LT; T&GT; ,在适当的时候

For even more flexibility, consider exposing some collections to IEnumerable<T>, when appropriate.

这篇关于为什么它认为不好的揭露列表&LT; T&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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