D
D
ddd3292020-12-13 18:44:51
.NET
ddd329, 2020-12-13 18:44:51

Why would ICollection re-inherit the IEnumerable interface in C#?

Hello!
C# has an ICollection(T) which is declared like this:

public interface ICollection<T> : IEnumerable<T>, IEnumerable
{
    int Count { get; }
    bool IsReadOnly { get; }
    void Add(T item);
    void Clear();
    bool Contains(T item);
    void CopyTo(T[] array, int arrayIndex);
    bool Remove(T item);
}

This collection inherits two interfaces IEnumerable(T) and IEnumerable. The question is why is the IEnumerable interface re-inherited if IEnumerable(T) already inherits the given interface?
public interface IEnumerable<out T> : IEnumerable
{
    IEnumerator<T> GetEnumerator();
}

Of course, I understand that this can be done and that there is nothing to worry about. But why? Maybe I don't understand something?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Ananiev, 2020-12-13
@SaNNy32

https://stackoverflow.com/questions/4972170/different...

V
Vasily Bannikov, 2020-12-14
@vabka

This is for backwards compatibility. Nothing wrong with that.
And yes - the behavior will not change if you remove the direct inheritance of IEnumerable

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question