Why does List<T> implement IList<T>, ICollection<T> and IEnumerable<T>?
Asked Answered
L

4

29

If you go to definition of List<T> you would see the following:

public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>

IList<T> already inherits from both ICollection<T> and IEnumerable<T>.

Wouldn't it have been sufficient if List<T> only implemented IList<T>?

Labialize answered 11/7, 2010 at 17:59 Comment(0)
A
18

Yes, it makes no difference in this case. In some cases it can make a difference, if you're using a base class which already implements an interface but you wish to reimplement it yourself explicitly - but in this case there's no base class (other than the implicit object) and it would have behaved exactly the same way.

Contrary to my recollections, I don't believe there's a difference in the way the class is represented in metadata whether the code explicitly declares all the interfaces or not. Here's an example:

interface IFoo {}
interface IBar : IFoo {}

class FooBar1 : IBar {}
class FooBar2 : IBar, IFoo {}

Both ildasm and Reflector show the same information for FooBar1 and FooBar2... it shows both of them implementing IBar and IFoo.

In other words, we can't tell whether the original source code for List<T> actually specifies all the interfaces or not. Maybe it does, maybe it doesn't - but it doesn't matter either way.

EDIT: For completeness, I also checked the cases where you're extending two interfaces with another interface. I can't find a difference in the metadata in that case, either. I'm sure I remember some situation in which it was evident, but I can't find it now.

Apterous answered 11/7, 2010 at 18:4 Comment(3)
So since there is no base class. It doesn't make a difference. Then why the developers inherited those two interface 'again'?Labialize
@Jon Skeet: I love you man. I want to implement your IBrain. Can I inherit "your" base class ?Tientiena
@WhileTrueSleep: I don't understand your comment, I'm afraid.Apterous
T
5

Yes it would. IList<T> itself implements the other two.

The object browser shows you all the interfaces the class implements, whether directly (IList<T>) or indirectly (ICollection<T>, IEnumerable<T>, through IList<T>).

Teenateenage answered 11/7, 2010 at 18:2 Comment(2)
you mean it's just a trick the object browser does to make things easier on us?Labialize
Well, if you want to put it that way. I really think it's because the browser's code simply lists all implemented interface, without caring to filter out those extended by others.Teenateenage
F
4

That's not the way it was actually coded behind the scenes. That's just what tools like Reflector show you when it turns the IL back into C#.

Foremast answered 11/7, 2010 at 18:5 Comment(1)
@Incognito: codeviewer generates it from metadata the same way reflector does.Needs
A
3

If you looked at the source code:

https://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs

You can observe that the signature is:

public class List<T> : IList<T>, System.Collections.IList, IReadOnlyList<T>

This can only mean that the metadata browser de-normalizes the inheritance hierarchy for us.

Alvie answered 17/1, 2019 at 3:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.