IEnumerable对象本质上是数组,对吧? [英] IEnumerable objects are essentially arrays, right?

查看:72
本文介绍了IEnumerable对象本质上是数组,对吧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了有关IEnumerable的msdn文档。在我看来,

IEnumerable对象本质上是包装数组。它简单地给了我们方便的东西。这是正确的吗?

I''ve read the msdn doc about IEnumerable. It seems to me that
IEnumerable objects are essentially wrapped-up arrays. It simply gives
us the foreach convenience. Is this correct?

推荐答案

gnewsgroup< gn ******** @ gmail.comwrote:
gnewsgroup <gn********@gmail.comwrote:

我已经阅读了有关IEnumerable的msdn文档。在我看来,

IEnumerable对象本质上是包装数组。它简单地给了我们方便的东西。它是否正确?
I''ve read the msdn doc about IEnumerable. It seems to me that
IEnumerable objects are essentially wrapped-up arrays. It simply gives
us the foreach convenience. Is this correct?



不,完全没有。


IEnumerable代表一系列项目 - 但不像数组:


1)它不提供随机访问;你基本上必须前进

(或重新开始)


2)它是只读的


3)IEnumerable是一个*接口* - 它可以以任何方式实现'

合适


4)数组总是完全在 - 记忆。 IEnumerable实现可以

枚举任何东西;例如,它可以枚举网络中的每一行

响应*,因为它到达*甚至不知道响应有多长。

哎呀,你可以枚举无限随机号码序列 - 当他们想要退出时,调用者就可以了。




< blatant plug>

请参阅 http://www.manning-source.com/ books / ... ter6sample.pdf for

更多关于迭代器模式。

< / blatant plug>


-

Jon Skeet - < sk *** @ pobox.com>

网站: http://www.pobox.com/~skeet

博客: http://www.msmvps.com/jon.skeet

C#深度: http:// csharpindep th.com

No, not at all.

IEnumerable represents a sequence of items - but unlike arrays:

1) It doesn''t provide random access; you basically have to go forwards
(or start again)

2) It''s read-only

3) IEnumerable is an *interface* - it can be implemented any way that''s
appropriate

4) Arrays are always totally in-memory. IEnumerable implementations can
enumerate anything; for instance, it could enumerate each line in a web
response *as it arrives* without even knowing how long the response is.
Heck, you could enumerate an infinite random number sequence - it''s up
to the caller to quit when they want to.

<blatant plug>
See http://www.manning-source.com/books/...ter6sample.pdf for
rather more about the iterator pattern.
</blatant plug>

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com


Jon Skeet [C#MVP]写道:
Jon Skeet [ C# MVP ] wrote:

gnewsgroup< gn ********@gmail.com写道:
gnewsgroup <gn********@gmail.comwrote:

我已经阅读了有关IEnumerable的msdn文档。在我看来,

IEnumerable对象本质上是包装数组。它简单地给了我们方便的东西。它是否正确?
I''ve read the msdn doc about IEnumerable. It seems to me that
IEnumerable objects are essentially wrapped-up arrays. It simply gives
us the foreach convenience. Is this correct?



不,完全没有。


IEnumerable代表一系列项目 - 但不像数组:


1)它不提供随机访问;你基本上必须前进

(或重新开始)


2)它是只读的


3)IEnumerable是一个*接口* - 它可以以任何方式实现'

合适


4)数组总是完全在 - 记忆。 IEnumerable实现可以

枚举任何东西;例如,它可以枚举网络中的每一行

响应*,因为它到达*甚至不知道响应有多长。

哎呀,你可以枚举无限随机号码序列 - 当他们想要退出时,调用者就可以了。




< blatant plug>

请参阅 http://www.manning-source.com/ books / ... ter6sample.pdf for

更多关于迭代器模式。

< / blatant plug>


-

Jon Skeet - < sk *** @ pobox.com>

网站: http://www.pobox.com/~skeet

博客: http://www.msmvps.com/jon.skeet

C#深度: http:// csharpindepth .com



谢谢。我知道IEnumerable是一个接口。我并不是说

IEnumerable对象和数组是完全相同的。我注意到

通常IEnumerable类的构造函数将数组作为参数的

,我认为foreach内部使用了MoveNext

方法。 IEnumerable对象的核心是一个

数组的情况吗?谢谢。

Thank you. I know IEnumerable is an interface. I didn''t mean
IEnumerable objects and arrays are exactly the same. I notice that
quite often the constructor of an IEnumerable class takes an array as
the argument, and I think foreach internally makes use of the MoveNext
method. Is it the case that the core of an IEnumerable object is an
array? Thanks.


5月12日,1:27 * am,gnewsgroup< gnewsgr ... @ gmail.comwrote:
On May 12, 1:27*am, gnewsgroup <gnewsgr...@gmail.comwrote:

谢谢。我知道IEnumerable是一个接口。 *我并不是说

IEnumerable对象和数组是完全相同的。 *我注意到

通常IEnumerable类的构造函数将数组作为参数的

,我认为foreach内部使用了MoveNext

方法。 IEnumerable对象的核心是一个

数组的情况吗?谢谢。
Thank you. I know IEnumerable is an interface. *I didn''t mean
IEnumerable objects and arrays are exactly the same. *I notice that
quite often the constructor of an IEnumerable class takes an array as
the argument, and I think foreach internally makes use of the MoveNext
method. Is it the case that the core of an IEnumerable object is an
array? Thanks.



不,不是。这一切都取决于您采取的具体实施方式。

IEnumerable。例如。数组本身是IEnumerable。列表< Tis

IEnumerable并且还包装一个数组。但LinkedList< Tis IEnumerable,

并不是一个数组,它也不像一个(你不能通过索引访问

元素)。另一个例子是.NET 3.5中的方法Enumerable.Range()

- 你给它一个下限和一个上限,并返回一个产生数字的IEnumerable的实现
实现在那个范围内。它将实现如下



class范围:IEnumerable< int>

{

private int lowerBound,upperBound;


public Range(int lowerBound,int upperBound)

{

this.lowerBound = lowerBound;

this.upperBound = upperBound;

}


public IEnumerator< intGetEnumerator()

{

for(int i = lowerBound; i< = upperBound; ++ i)

{

收益率i;

}

}

}


你可以注意到,这里根本没有数组。 ..


IEnumerable所代表的概念实际上是一个序列。 (a $

有限或无限的 - 没有人说MoveNext()曾经必须

返回false!)。 "阵列"是一个更具体的概念 - 引用

维基百科,在计算机科学中,数组是一种数据结构

由一组通过索引访问的元素组成。在

大多数编程语言中,每个元素具有相同的数据类型,并且

数组占据连续的存储区域。显然,任何数组都是一个序列,但不是每个序列都是一个数组,甚至不是数组类似的

(因为不是每个序列都可以被索引,有些 - 例如

字典< Tand HashSet< T-甚至没有任何具体的排序。

No, it is not. It all depends on which particular implementation of
IEnumerable you take. E.g. array itself is IEnumerable. List<Tis
IEnumerable and also wraps an array. But LinkedList<Tis IEnumerable,
and is not an array, nor does it behave like one (you can''t access
elements by index). Another example would be method Enumerable.Range()
in .NET 3.5 - you give it a lower and an upper bound, and returns an
implementation of IEnumerable that produces numbers in that range. It
would be implemented something like this:

class Range : IEnumerable<int>
{
private int lowerBound, upperBound;

public Range(int lowerBound, int upperBound)
{
this.lowerBound = lowerBound;
this.upperBound = upperBound;
}

public IEnumerator<intGetEnumerator()
{
for (int i = lowerBound; i <= upperBound; ++i)
{
yield return i;
}
}
}

As you can notice, there are no arrays here at all...

The concept that IEnumerable represents is actually a "sequence" (a
finite or an infinite one - noone says that MoveNext() ever has to
return false!). "Array" is a more specific concept - to quote
Wikipedia, "In computer science an array is a data structure
consisting of a group of elements that are accessed by indexing. In
most programming languages each element has the same data type and the
array occupies a contiguous area of storage.". Obviously, any array is
also a sequence, but not every sequence is an array or even array-like
(since not every sequence can be indexed, and some - e.g.
Dictionary<Tand HashSet<T- do not even have any specific ordering).


这篇关于IEnumerable对象本质上是数组,对吧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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