关于IEnumerable和IEnumerator的问题 [英] Question regarding IEnumerable and IEnumerator

查看:179
本文介绍了关于IEnumerable和IEnumerator的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的代码,以使MyClass的使用的foreach。但我相当新的编程并在理解下面的代码一定的难度。我描述了在评论我的问题。我会提供一些​​信息感激

 公共类MyClass的:IEnumerable的<串GT; 
{
// 1)什么是IEnumerator的呢?
//请告诉我的IEnumerator和IEnumerable
公众的IEnumerator℃之间的差异;串>的GetEnumerator()
{
收益率的回报第一;
收益率的回报第二;
}
// 2)这是什么呢?它只是调用上面的方法
的IEnumerator IEnumerable.GetEnumerator()
{
返回的GetEnumerator();
}
}
// 3)最后,我从实现遗传接口
// IEnumerable的<有什么好处;串>而不是仅仅IEnumerable的


解决方案

的IEnumerator和IEnumerable之间的区别是什么?




贾森的回答是不错,但我想我只是添加我如何看待这一点。想象一下,你有一个序列:

  1,1,2,3,5,8,13,... 

现在想象你在该序列的某个位置有一个箭头指向:

  1,1,2,3,5,8,13,... 
^

这是箭是一个对象,可以做两件事情。首先,它可以让你在它指向的东西。第二,它可以使自己指向未来的事情。



IEnumerator的是一个箭头。它有一个属性,目前,该让你在它指向的东西。它具有使自己指向接下来的事情的方法,调用MoveNext()。



你如何摆在首位的箭头?你需要一个箭头工厂。你问工厂的箭头,它给你一个指向序列中的第一个元素一个箭头。



IEnumerable的是一个箭头的工厂。它有一个方法,GetEnumerator的,是给你一个箭头,该序列的第一个元素。



本方案的一个很好的特性是,你可以有指向多重箭以相同的顺序不同的地方。




是什么实现通用接口的IEnumerable,而不是仅仅IEnumerable的

$ b $的好处b

假设序列是一个整数。如果实施的IEnumerable 然后当你说

 的foreach(INT中X mysequence)

这实际上做的是什么转换INT序列中的对象,拳击整数,然后立即拆箱对象回整,加入完全不必要的内存分配到每一个操作。如果编译器知道该序列是整数的话,就可以跳过不必要的装箱操作。



假设序列是一个字符串。如果实施的IEnumerable<串> ,那么你可以说:

 字符串第一= mysequence.First(); 

如果你没有,那么你不得不说

 字符串第一个=(字符串)mysequence.First(); 



这是不必要的,容易出错。而不是通过强制转换的类型为字符串命令编译器,你可以简单的保证的该类型的字符串使用的类型系统。


I use the following code to enable myClass to use foreach. But I am rather new to programming and have some difficulty in understanding the following code. I described my problems in the comments. I would be grateful for providing some information.

    public class MyClass : IEnumerable<string> 
    {  
    //1) What is IEnumerator for?
        // Whats the difference between IEnumerator and IEnumerable
    public IEnumerator<string> GetEnumerator()     
    {
             yield return "first";         
             yield return "second";     
    }      
    //2) What is it for?  It just calls above method 
   IEnumerator IEnumerable.GetEnumerator()
         {
             return GetEnumerator(); 
         } 
    }
   //3) Lastly what benefits I have from implementing genetic interface 
   //IEnumerable<string> instead of just IEnumerable

解决方案

What is the difference between IEnumerator and IEnumerable?

Jason's answer is good but I thought I'd just add how I think about this. Imagine you have a sequence:

1, 1, 2, 3, 5, 8, 13, ...

Now imagine you have an arrow pointing at some position of that sequence:

1, 1, 2, 3, 5, 8, 13, ...
         ^

An "arrow" is an object that can do two things. First, it can give you the thing it is pointing at. Second, it can make itself point at the next thing.

IEnumerator is an arrow. It has a property, Current, that gives you the thing it is pointing at. It has a method, MoveNext() that makes itself point at the next thing.

How do you get an arrow in the first place? You need an arrow factory. You ask the factory for an arrow, and it gives you an arrow that points to the first element in the sequence.

IEnumerable is an arrow factory. It has a method, GetEnumerator, that gives you an arrow to the first element of the sequence.

A nice property of this scheme is that you can have multiple arrows pointing to different places in the same sequence.

what are the benefits of implementing generic interface IEnumerable instead of just IEnumerable?

Suppose the sequence is of integers. If you implement IEnumerable then when you say

foreach(int x in mysequence)

what that will actually do is convert the int in the sequence to object, boxing the integer, and then immediately unbox the object back to integer, adding a completely unnecessary memory allocation to every single operation. If the compiler knows that the sequence is of integers then it can skip the unnecessary boxing operation.

Suppose the sequence is of strings. If you implement IEnumerable<string> then you can say:

string first = mysequence.First();

If you don't, then you have to say

string first = (string)mysequence.First();

which is unnecessary and error-prone. Rather than instruct the compiler via a cast that the type is string, you can simply guarantee that the type is string by using the type system.

这篇关于关于IEnumerable和IEnumerator的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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