返回'的IList'VS'ICollection的'VS'收集' [英] Returning 'IList' vs 'ICollection' vs 'Collection'

查看:123
本文介绍了返回'的IList'VS'ICollection的'VS'收集'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感到困惑的哪个集合类型,我应该从我的公共API的方法和属性返回。

I am confused about which collection type that I should return from my public API methods and properties.

这是我心目中的集合是的IList 的ICollection 收藏

The collections that I have in mind are IList, ICollection and Collection.

时返回这些类型之一总是$ pferred在别人p $,或者它取决于具体的情况?

Is returning one of these types always preferred over the others, or does it depend on the specific situation?

推荐答案

通常你应该返回一个类型,是尽可能通用,那知道刚好够返回的数据,消费者需要使用,即之一。你有更大的自由通过这种方式来改变API的实现,而不会破坏正在使用它的code。

Generally you should return a type that is as general as possible, i.e. one that knows just enough of the returned data that the consumer needs to use. That way you have greater freedom to change the implementation of the API, without breaking the code that is using it.

也考虑的IEnumerable< T> 接口返回类型。如果结果只会迭代,消费者并不需要更多的。

Consider also the IEnumerable<T> interface as return type. If the result is only going to be iterated, the consumer doesn't need more than that.

这篇关于返回'的IList'VS'ICollection的'VS'收集'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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