IEnumerable T与IReadOnlyList< T> [英] IEnumerable<T> vs IReadOnlyList<T>
问题描述
选择IEnumerable<T>
与IReadOnlyList<T>
作为返回参数类型或输入参数类型有什么区别?
What is the difference between choosing IEnumerable<T>
vs IReadOnlyList<T>
as a return parameter type or input parameter type?
IEnumerable<T>
提供.Count
和.ElementAt
,这是IReadOnlyList<T>
推荐答案
IEnumerable<T>
表示某些数据上的仅前向游标.您可以从收藏的开头到结尾,一次查看一个项目.
IEnumerable<T>
represents a forward-only cursor over some data. You can go from start to end of the collection, looking at one item at a time.
IReadOnlyList<T>
表示可读的随机访问集合.
IReadOnlyList<T>
represents a readable random access collection.
IEnumerable<T>
更通用,因为它可以表示动态生成的项目,通过网络传入的数据,来自数据库的行等.另一方面,IReadOnlyList<T>
基本上仅表示内存中的集合.
IEnumerable<T>
is more general, in that it can represent items generated on the fly, data coming in over a network, rows from a database, etc. IReadOnlyList<T>
on the other hand basically represents only in-memory collections.
如果您只需要按顺序查看一次,那么IEnumerable<T>
是更好的选择-它更通用.
If you only need to look at each item once, in order, then IEnumerable<T>
is the superior choice - it's more general.
我建议实际上看看C ++标准模板库-他们对各种类型的迭代器的讨论实际上可以很好地映射到您的问题.
I'd recommend actually looking at the C++ Standard Template Library - their discussion of the various types of iterators actually maps pretty well to your question.
这篇关于IEnumerable T与IReadOnlyList< T>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!