Explicit implementation of the GetEnumerator interface causes stack overflow

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()
    {
        // This calls itself over and over
        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);
}
+5
6

:

    public class Things : List<Thing>, IThings
    {
        IEnumerator<IThing> IEnumerable<IThing>.GetEnumerator()
        {
            foreach (Thing t in this)
            {
                yield return t;
            }
        }
    }

.

+3
IEnumerator<IThing> IEnumerable<IThing>.GetEnumerator()
{
    // This calls itself over and over
    return this.Cast<IThing>().GetEnumerator();
}

, , .

, , . this.GetEnumerator(); , , , . , base.GetEnumerator();

+1

, , , Things Thing. Things IThing, Thing.

:

public class Things : List<IThing>, IThings
{
}

GetEnumerator() , . , , , .

+1

, .

# 4

IEnumerator<IThing> IEnumerable<IThing>.GetEnumerator()
{
    return base.GetEnumerator();
}
+1

, this.Cast<IThing>() IEnumerable. , , Cast.

0

, , . . P GetEnumerator()?

, List, GetEnumerator.

, . , IEnumerable <() IThings > ( , , List ), List, IEnumerable, IList List. , ( ).

0

All Articles