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.