IList的VS ICollection的VS系列 [英] IList vs ICollection vs Collection

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

问题描述

我只是困惑保持输出类型,我的公开API的方法。你能告诉我为什么,哪些类型,我应该在这些使用:IList的VS ICollection的VS系列的方法我的返回类型

I was just confused for keeping the output type for my public API methods. Can you tell me why and which Type should I use among these: IList vs ICollection vs Collection for my return type of methods?

推荐答案

通常你应该返回一个类型,是尽可能地通用,即一个知道刚够返回的数据,消费者需要使用。这样,你有更多的自由来改变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天全站免登陆