locked
IEnumerable implementation RRS feed

  • Question

  • Dear all,
    I get one simple question based on the ForEach instruction.
    let say that I create my own object class and I will like that the ForEAch works fine with my new object.
    What is the minimum requirement for my new class in order to work ?

    Is it enough that it implements the IEnumerable interface ?
    Or does it needs others  ?

    thnaks for help
    regards
    serge
    Your experience is build from the one of others
    Monday, June 2, 2008 6:54 AM

Answers

  • Suppose that your internal collection of Car is called "_carCollection".
    You can use the magic of "yield" to return each value in that collection like this:

    class Cars: IEnumerable<Car> 

       ... Other stuff like CTOR

        public IEnumerator<Car> GetEnumerator() 
        { 
              foreach (Car car in _carCollection)
              {
                    yield return car;
              }
        } 

        IEnumerator IEnumerable.GetEnumerator() 
        { 
            return GetEnumerator(); 
        } 
    }


    Basically "yield" works differently from other C# things. It generates a lot of management code in the background to make implementing GetEnumerator() much easier.

    Basically, you have to write GetEnumerator() so that it does "yield return xxxx" for each item that you want to enumerate, in the order that you  want them to be enumerated.
    • Marked as answer by jack 321 Thursday, June 5, 2008 6:07 AM
    Monday, June 2, 2008 10:51 AM
  • For full details about GetEnumerator(), what it means, and how you can implement it both the easy way and hard way, see here.
    Marc
    • Marked as answer by jack 321 Thursday, June 5, 2008 6:07 AM
    Monday, June 2, 2008 11:19 AM

All replies

  • Actually, *technically* it doesn't even need that - just a suitable GetEnumerator() method that returns something with suitable MoveNext() and Current members. However, in reality, yes: implenting IEnumerable or IEnumerable<T> would be the norm (with the latter being preferable since it provides additional compiler support).

    Note that you don't have to do much work to implement IEnumerable, since you can typically just forward the enumerator for an existing list (in trivial cases), or use an "iterator block" (aka "yield return") for more complex cases - i.e.

        class Foo : IEnumerable<int>  
        {  
            public IEnumerator<int> GetEnumerator()  
            {  
                yield return 1;  
                yield return 3;  
                yield return 5;  
            }  
            IEnumerator IEnumerable.GetEnumerator()  
            {  
                return GetEnumerator();  
            }  
        } 


    Marc
    Monday, June 2, 2008 7:04 AM
  •  Thnaks for your reply..
    hmmm I do not see really what could be inside my GetEnumerator method ?
    What is your yield return 1, yield return 2 ??

    Lets say that my object named Cars contains a collection of Car with member Color and Type..
    What would be the GetEnumerator looks like ?

    Thnaks for your help
    serge
    Your experience is build from the one of others
    Monday, June 2, 2008 10:05 AM
  • Suppose that your internal collection of Car is called "_carCollection".
    You can use the magic of "yield" to return each value in that collection like this:

    class Cars: IEnumerable<Car> 

       ... Other stuff like CTOR

        public IEnumerator<Car> GetEnumerator() 
        { 
              foreach (Car car in _carCollection)
              {
                    yield return car;
              }
        } 

        IEnumerator IEnumerable.GetEnumerator() 
        { 
            return GetEnumerator(); 
        } 
    }


    Basically "yield" works differently from other C# things. It generates a lot of management code in the background to make implementing GetEnumerator() much easier.

    Basically, you have to write GetEnumerator() so that it does "yield return xxxx" for each item that you want to enumerate, in the order that you  want them to be enumerated.
    • Marked as answer by jack 321 Thursday, June 5, 2008 6:07 AM
    Monday, June 2, 2008 10:51 AM
  • For full details about GetEnumerator(), what it means, and how you can implement it both the easy way and hard way, see here.
    Marc
    • Marked as answer by jack 321 Thursday, June 5, 2008 6:07 AM
    Monday, June 2, 2008 11:19 AM