IEnumerable, IEnumerator по сравнению с foreach, когда использовать что [дубликат]

Используйте getResources().getConfiguration().orientation, это правильный путь.

Вам просто нужно следить за различными типами ландшафта, ландшафтом, который обычно использует устройство, и другим.

Все еще не понимаю, как с этим справиться.

23
задан Wondering 6 July 2009 в 06:13
поделиться

3 ответа

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;
    }
}

Now, if you're interested in the difference between IEnumerator and IEnumerable, you might want to think of it in database terms: think of IEnumerable as a table, and IEnumerator 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.

50
ответ дан 29 November 2019 в 01:05
поделиться

What Jon said.

  • IEnumerable or IEnumerable : by implementing this an object states that it can give you an iterator that you can use to traverse over the sequence/collection/set
  • IEnumerator or IEnumerator : if you call the GetEnumerator method defined in the previous interface, you get an iterator object as an IEnumerator reference. This enables you to call MoveNext() and get the Current object.
  • foreach : is a C# construct/facade in a sense in that you don't need to know how it works under the hood. It internally gets the iterator and calls the right methods for you to concentrate on what you want to do with each item (the contents of the foreach block). Most of the time, you'd just need foreach unless you're implementing your own type or custom iteration, in which case you'd need to get to know the first 2 interfaces.
8
ответ дан 29 November 2019 в 01:05
поделиться

Имейте в виду, что у вас нет для реализации IEnumerator и его вариантов для использования foreach - IIRC, все, что ему нужно, это метод GetEnumerator (), который возвращает объект у которого есть метод MoveNext (), возвращающий bool, и свойство Current, возвращающее объект. Вам не нужно использовать IEnumerator и IEnumerable, хотя обычно это очень хорошая идея. См. здесь для получения дополнительной информации.

6
ответ дан 29 November 2019 в 01:05
поделиться
Другие вопросы по тегам:

Похожие вопросы: