I do my best to code with interfaces as much as possible, but I am having some problems when it comes to collections. For example, here are a couple of interfaces that I would like to use.
public interface IThing {}
public interface IThings : IEnumerable<IThing> {}
Here are the implementations. To implement IEnumerable <IThing> I need to explicitly implement IEnumerable <IThing> .GetEnumerator () in Things.
public class Thing : IThing {}
public class Things : List<Thing>, IThings
{
IEnumerator<IThing> IEnumerable<IThing>.GetEnumerator()
{
return this.Cast<IThing>().GetEnumerator();
}
}
The problem is that the GetEnumerator implementation causes a stack overflow. He calls himself again and again. I cannot understand why he decided to call this implementation GetEnumerator instead of the implementation provided by the result of this. Cast <IThing> (). Any ideas what I'm doing wrong? I bet on something very stupid ...
:
static void Enumerate(IThings things)
{
foreach (IThing thing in things)
{
Console.WriteLine("You'll never get here.");
}
}
static void Main()
{
Things things = new Things();
things.Add(new Thing());
Enumerate(things);
}