Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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>?

like image 926
Marco M. Avatar asked Jul 11 '10 17:07

Marco M.


People also ask

Does list t implement IEnumerable?

List implements IEnumerable, but represents the entire collection in memory. LINQ expressions return an enumeration, and by default the expression executes when you iterate through it using a foreach, but you can force it to iterate sooner using . ToList() or . ToArray().

Does List implement ICollection?

List is the concrete class. This class implements IList, ICollection, IEnumerable.

What is difference between IList and IEnumerable?

IList doesn't support further filtering. IEnumerable exists in System. Collections Namespace. IEnumerable is a forward only collection, it can't move backward and between the items.

What is the use of IEnumerable ICollection IList and IDictionary in C#?

To begin with, all the interfaces (ICollection, IList, IQueryable, IDictionary) inherit from IEnumerable, which allows us to use the foreach statement. ICollection: is the most basic interface among these, it is enumerable and it supports Count operator. IQueryable: an enumerable interface that supports LINQ.


2 Answers

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.

like image 137
Jon Skeet Avatar answered Sep 19 '22 20:09

Jon Skeet


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>).

like image 37
Mau Avatar answered Sep 19 '22 20:09

Mau