的IEnumerable,IEnumerator的VS的foreach,何时使用什么 [英] IEnumerable , IEnumerator vs foreach, when to use what

查看:98
本文介绍了的IEnumerable,IEnumerator的VS的foreach,何时使用什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在经历的IEnumerable和IEnumerator的,但不能得一分clearly..if我们的foreach,那么为什么我们需要这两个接口?有没有在这里我们不得不使用interfaces.If肯定的,那么有人可以用一​​个例子说明任何情况。
任何建议和意见是值得欢迎的。
感谢。

I was going through IEnumerable and IEnumerator , but could not get one point clearly..if we have foreach, then why do we need this two interfaces? Is there any scenario where we have to use interfaces.If yes, then can somebody explain with an example. Any suggestions and remarks are welcome. Thanks.

推荐答案

的foreach 使用的在许多情况下的接口。如果你想的实施的一个序列的foreach 就可以使用您需要的接口。 (迭代器块通常使这个任务的实现非常简单,但。)

foreach uses the interfaces in many cases. You need the interfaces if you want to implement a sequence which foreach can then use. (Iterator blocks usually make this implementation task very simple though.)

然而,仅仅偶尔的它可以直接使用迭代器有用。一个很好的例子是试图配对两个不同的序列时。例如,假设您收到两个序列 - 名字之一,年龄之一,要打印两者结合起来。你可能会这样写:

However, just occasionally it can be useful to use the iterators directly. A good example is when trying to "pair up" two different sequences. For example, suppose you receive two sequences - one of names, one of ages, and you want to print the two together. You might write:

static void PrintNamesAndAges(IEnumerable<string> names, IEnumerable<int> ages)
{
    using (IEnumerator<int> ageIterator = ages.GetEnumerator())
    {
        foreach (string name in names)
        {
            if (!ageIterator.MoveNext())
            {
                throw new ArgumentException("Not enough ages");
            }
            Console.WriteLine("{0} is {1} years old", name, ageIterator.Current);
        }
        if (ageIterator.MoveNext())
        {
            throw new ArgumentException("Not enough names");
        }

    }
}



同样它可如果你想治疗(说)不同的使用迭代器的第一个项目,以受用:

Likewise it can be useful to use the iterator if you want to treat (say) the first item differently to the rest:

public T Max<T>(IEnumerable<T> items)
{
    Comparer<T> comparer = Comparer<T>.Default;

    using (IEnumerator<T> iterator = items.GetEnumerator())
    {
        if (!iterator.MoveNext())
        {
            throw new InvalidOperationException("No elements");
        }
        T currentMax = iterator.Current;

        // Now we've got an initial value, loop over the rest
        while (iterator.MoveNext())
        {
            T candidate = iterator.Current;
            if (comparer.Compare(candidate, currentMax) > 0)
            {
                currentMax = candidate;
            }
        }
        return currentMax;
    }
}





现在,如果你有兴趣的区别的IEnumerator< T> 的IEnumerable< T> ,您可能希望在数据库方面的意见:认为的IEnumerable<的; T> 作为一个表,而的IEnumerator< T> 作为光标。你可以要求一个表给你一个新的光标,你可以有超过在同一时间同一个表的多个游标。

Now, if you're interested in the difference between IEnumerator<T> and IEnumerable<T>, you might want to think of it in database terms: think of IEnumerable<T> as a table, and IEnumerator<T> as a cursor. You can ask a table to give you a new cursor, and you can have multiple cursors over the same table at the same time.

这可能需要一段时间才能真正神交这种差异,而只是记住一个列表(或数组,或其他)不具有的你在哪里列表中的任何概念,但在该列表/数组/迭代器无论确实的有状态该位是很有帮助的。

It can take a while to really grok this difference, but just remembering that a list (or array, or whatever) doesn't have any concept of "where you are in the list" but an iterator over that list/array/whatever does have that bit of state is helpful.

这篇关于的IEnumerable,IEnumerator的VS的foreach,何时使用什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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