Brandon Moore
Brandon Moore

Reputation: 8780

Iterate over your own objects (the easy way...)

It seems like I remember an interface that can be implemented that basically has one method that has to return an IEnumerable object, and implementing this interface will allow you use foreach over your object. Can someone tell me what this interface is, or correct me if I'm mis-remembering about this?

Edit: Sorry guys, I just realized I'm mixing two things up in my head. I don't think what I just asked for exists, but what I was (and am still) trying to think of is an interface you can implement instead of either IList or IEnumerable (I forget which) that has a method which lets you just return an object of that type rather than actually implementing the IList (or IEnumerable?) interface.

So... slightly different question but still just as relevant to me.

EDIT: IListSource is what I was trying to think of. Sorry everyone for the poorly thought out question. Ah well, they can't all be good :)

Upvotes: 1

Views: 170

Answers (5)

phoog
phoog

Reputation: 43046

Iterator blocks allow you to "yield return" objects of type T rather than creating a type that implements IEnumerable<T>. For example:

IEnumerable<int> PrimesLessThanTen()
{
    yield return 2;
    yield return 3;
    yield return 5;
    yield return 7;
}

or

IEnumerable<byte> OddBytes()
{
    byte b = 1;
    do
    {
        yield return b;
        b += 2;
    } while (b != 1);
}

Upvotes: 0

Amar Palsapure
Amar Palsapure

Reputation: 9680

Objects which implements IEnumberable (generic IEnumberable<T>) need to implement GetEnumerator. Using the method you get strongly typed IEnumerator and in case of IEnumberable<T> using method you will get generic IEnumerator.

IEnumerator provides methods like

  • MoveNext which yields the next item in the collection
  • Reset which resets the enumerator to its initial position, which is before the first element in the collection.

using which you can iterate over the collection.

Upvotes: 0

Guffa
Guffa

Reputation: 700362

The EInumerable<T> interface (and the non-generic IEnumerable interface) is used to make classes enumerable.

You can implement IEnumerable<T> like this:

public class MyClass : IEnumerable<Item> {

  public IEnumerator<Container<T>> GetEnumerator() {

    // here you return your items, for example by returning an enumerator:
    return someArray.GetEnumerator();

    // or by using yield return:
    for (int i = 0; i < 10; i++) {
      yield return new Item(i);
    }

  }

  // this is needed, because IEnumerable<T> inherits IEnumerable
  System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
    // uses the generic implementation
    return GetEnumerator();
  }

}

Edit:

The foreach statement uses duck typing, and not the IEnumerable<T> interface, so you don't really need to implement the interface to use a class in a foreach statement. You just implement the GetEnumerator method.

Upvotes: 0

Esben Skov Pedersen
Esben Skov Pedersen

Reputation: 4517

IEnumerable<T> has one method to return an IEnumerator<T>

IEnumerator<T> has methods like MoveNext and Current

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500665

It's IEnumerable (or the generic equivalent, IEnumerable<T>) which has a GetEnumerator method, returning either IEnumerator or IEnumerator<T>.

When you implement that method, you can either call GetEnumerator on another collection (e.g. a list within your own class) or you can use iterator blocks to perform more custom iteration.

If you run into any problems doing what you need to, post more details and we can help more.

Upvotes: 0

Related Questions