1

I have a class that wraps a list of objects. I would like this class to be compatible with the foreach loop.

I have seen several questions about how to do that on SO, but most of the answers do not compile for me.

class TrackList
{
    private List<Track> tracks = new List<Track>();
}

What would you add to this to make it usable with for each ?

So far I have :

class TrackList : IEnumerable<Track>
{
    // ...

    public IEnumerator<Track> GetEnumerator()
    {
        return trackList.GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerator.GetEnumerator()
    {
        return this.GetEnumerator();
    }
}

But it says (translated error) :

Error 1 'HP.TrackList' does not implement the interface member 'System.Collections.IEnumerable.GetEnumerator()'. 'HP.TrackList.GetEnumerator()' cannot implement 'System.Collections.IEnumerable.GetEnumerator()', because it doesn't have the return type corresponding to 'System.Collections.IEnumerator'. XXX\TrackList.cs 11

If instead I use :

IEnumerator<Track> IEnumerator<Track>.GetEnumerator()
{
    return this.GetEnumerator();
}

Is giving me the same error.

krillgar
  • 12,596
  • 6
  • 50
  • 86
Virus721
  • 8,061
  • 12
  • 67
  • 123
  • Why do you need to iterate over the class, instead of a property of the class? – krillgar Jan 27 '16 at 15:58
  • Because for some other reason I don't want to make tracks avaialble as a property. You can iterate over a list without accessing a property of that list that would be called Items for example. I want to iterate over my tracklist without accessing a property class (for example) Tracks. – Virus721 Jan 27 '16 at 15:59
  • 2
    Try using `System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()` instead. – Lasse V. Karlsen Jan 27 '16 at 16:00
  • Check out this tutorial: https://msdn.microsoft.com/en-us/library/xth2y6ft(v=vs.71).aspx – Aaron Jan 27 '16 at 16:01
  • @LasseV.Karlsen Thanks it works. Why do I need to specifiy the full path to the class ? This `IEnumerator IEnumerable.GetEnumerator` is asking for a type parameter. EDIT: Well nevermind, I just had using `using System.Collections.Generic;` but not using `System.Collections;` – Virus721 Jan 27 '16 at 16:02
  • @Virus721 Probably because you don't have a `using System.Collections;` at the top of your file. But you're probably not using that namespace anywhere else, so it's easier to specify it at the method. – Dennis_E Jan 27 '16 at 16:07

1 Answers1

7

This line is wrong:

System.Collections.IEnumerator System.Collections.IEnumerator.GetEnumerator()

You have two types here:

System.Collections.IEnumerator System.Collections.IEnumerator.GetEnumerator()
^----------------------------^ ^----------------------------^
  The type you're returning       The interface you're
                                      implementing

The second should be IEnumerable not IEnumerator so make it:

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()

Also note that your private field:

private List<Track> tracks = new List<Track>();

is named tracks and not trackList, I'm assuming this is a simply typo, otherwise you need to fix this as well.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • Thanks for your answer. It is nammed `tracks` because the track list is the object itself, so I think it makes more sense. `trackList` is what I use for instances of this class. – Virus721 Jan 27 '16 at 16:05