为什么它认为不好的揭露列表&LT; T&GT ;? [英] Why is it considered bad to expose List<T>?
问题描述
据FxCop的,名单不应该在一个API对象模型公开。这是为什么做不好?
According to FXCop, List should not be exposed in an API object model. Why is this considered bad practice?
推荐答案
我同意驼鹿式的非丛林这里:列表&LT; T&GT;
是一个不受约束,臃肿的对象,在它有很多包袱的。
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屋!