You want to aim for the least possible coupling, so return an IEnumerable<T> if that is enough. It probably will be.
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.
It depends what you want to do with the result. If you need to get the count of items or get random access to individual items, go with an IList.
If callers just want to iterate through the items then go with IEnumerable - but you should document whether or not the returned value will be evaluated lazily or not - many IEnumerable instances these days represent queries that will be executed when the collection is enumerated. To be on the safe side, if what you are returning won't be evaluated on demand, I'd go with IList.
Generally, it's better to return IEnumerable<T>, as long as that has everything the caller needs.
IEnumerable<T> is foreachable, which is all that's needed for many consumers. It's also read-only, which is often a good thing -- it means you can sometimes optimize by returning your actual backing collection, without worrying too much about someone modifying it without telling you.
However, if the consumer needs methods that aren't on IEnumerable<T>, then IList<T> might make more sense. For example, the caller may want to call Contains, which isn't on IEnumerable<T>. Or the caller may want to index into the list, rather than iterating it from start to finish.
But you can do Contains and indexing on IEnumerable<T> too, via LINQ's Contains and ElementAt extension methods. So there's a question of degree here. If the caller only needs to ask one question that isn't available on IEnumerable<T>, like "is this empty", then return an IEnumerable<T> and use the Any extension method. But if the caller uses IList<T> operations extensively, return IList<T>.
发布评论
评论(7)
这里有一个层次结构:
您希望以尽可能少的耦合为目标,因此如果足够的话,请返回
IEnumerable
。可能会的。如果情况要求调用者获取可用于添加/插入/删除的列表,则返回
IList
。但即便如此,如果调用者从 IEnumerable 集合创建自己的 List 可能会更好。There is a hierarchy here:
You want to aim for the least possible coupling, so return an
IEnumerable<T>
if that is enough. It probably will be.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.这取决于您想对结果做什么。如果您需要获取项目的计数或随机访问单个项目,请使用 IList。
如果调用者只想迭代这些项目,则使用 IEnumerable - 但您应该记录返回的值是否会被延迟计算 - 如今许多 IEnumerable 实例表示将在以下情况下执行的查询:集合被枚举。为了安全起见,如果您返回的内容不会根据需要进行评估,我会选择 IList。
It depends what you want to do with the result. If you need to get the count of items or get random access to individual items, go with an IList.
If callers just want to iterate through the items then go with IEnumerable - but you should document whether or not the returned value will be evaluated lazily or not - many IEnumerable instances these days represent queries that will be executed when the collection is enumerated. To be on the safe side, if what you are returning won't be evaluated on demand, I'd go with IList.
一般来说,最好返回
IEnumerable
,只要它包含调用者所需的一切。IEnumerable
是 foreachable,这就是许多消费者所需要的。它也是只读的,这通常是一件好事——这意味着您有时可以通过返回实际的后备集合来进行优化,而不必太担心有人在不告诉您的情况下修改它。但是,如果使用者需要
IEnumerable
上没有的方法,则IList
可能更有意义。例如,调用者可能想要调用Contains
,但它不在IEnumerable
上。或者调用者可能希望对列表进行索引,而不是从头到尾迭代它。但您也可以通过 LINQ 的 执行包含和索引操作"noreferrer">包含 和 ElementAt 扩展方法。所以这里有一个程度的问题。如果调用者只需询问
IEnumerable
上无法提供的一个问题,例如“这是空的吗”,则返回一个IEnumerable
并使用任何 扩展方法。但如果调用者广泛使用IList
操作,则返回IList
。Generally, it's better to return
IEnumerable<T>
, as long as that has everything the caller needs.IEnumerable<T>
is foreachable, which is all that's needed for many consumers. It's also read-only, which is often a good thing -- it means you can sometimes optimize by returning your actual backing collection, without worrying too much about someone modifying it without telling you.However, if the consumer needs methods that aren't on
IEnumerable<T>
, thenIList<T>
might make more sense. For example, the caller may want to callContains
, which isn't onIEnumerable<T>
. Or the caller may want to index into the list, rather than iterating it from start to finish.But you can do Contains and indexing on
IEnumerable<T>
too, via LINQ's Contains and ElementAt extension methods. So there's a question of degree here. If the caller only needs to ask one question that isn't available onIEnumerable<T>
, like "is this empty", then return anIEnumerable<T>
and use the Any extension method. But if the caller usesIList<T>
operations extensively, returnIList<T>
.这很容易,
如果调用者只应使用 Readonly,请使用 IEnumerable。
因为这也支持协方差(结果可以转换为基本类型)
Its easy,
if the caller should only use it Readonly, use IEnumerable.
as this is then also supports covariance (result can be casted to a base type)
如果你想返回一个有序列表,也许你应该返回一个 SortedList。
http://msdn.microsoft.com/en-us/ library/system.collections.sortedlist.aspx
您可以将顺序与对象关联起来。
If you want to return an ordered list maybe you should return a SortedList.
http://msdn.microsoft.com/en-us/library/system.collections.sortedlist.aspx
You can associate an order with the objects.
IEnumerable 不如 IList 具体,即 IList 具有 IEnumerable 没有的功能。
比较两者,看看其中一个是否具有您需要的功能,而另一个则没有。
IEnumerable is less specific than an IList, that is, IList has functions that IEnumerable does not.
Compare the two to see if one has functions you need that the other does not.
IList 具有更改项目的方法(如 Add),也许您想在 ICollection 和 IEnumerable 之间进行选择。
ICollection 扩展了 IEnumerable 并具有有用的 Count 属性。
An IList has the methods for changing the items (like Add), maybe you want to select between ICollection and IEnumerable.
The ICollection extends IEnumerable and has the Count property available that can be useful.