我应该返回一个IEnumerable或IList的? [英] Should I return an IEnumerable or IList?
本文介绍了我应该返回一个IEnumerable或IList的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从方法返回项目的有序列表。 ?如果我的返回类型的IEnumerable或IList的
I wish to return an ordered list of items from a method. Should my return type be IEnumerable or IList?
推荐答案
有一个层次的位置:
interface IList<T> : ICollection<T> { }
interface ICollection<T> : IEnumerable<T> { }
您想瞄准尽可能少的耦合,所以返回 IEnumerable的< T>
如果这是不够的。它可能会。
You want to aim for the least possible coupling, so return an IEnumerable<T>
if that is enough. It probably will be.
返回一个的IList< T>
如果情况需要,调用者得到它可以用它来添加列表/插入/删除。但即便如此,如果主叫方的IEnumerable集合创建了自己的名单可能会更好。
Return an IList<T>
if the situation requires that the caller gets a List that it can use to Add/Insert/Remove. But even then it might be better if the caller created his own List from the IEnumerable collection.
这篇关于我应该返回一个IEnumerable或IList的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文