5

Why is IList defined like this?

public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable

public interface ICollection<T> : IEnumerable<T>, IEnumerable

public interface IEnumerable<T> : IEnumerable

Couldn't it just be

public interface IList<T> : ICollection<T>

So, to test I created these interfaces, just to be sure if that works!

public interface IOne
{
    string One();
}

public interface ITwo : IOne
{
    string Two();
}

public interface IThree : ITwo, IOne
{
    string Three();
}

While its perfectly fine, Resharper complains about "Redundant interfaces".

Any ideas why Microsoft went on with this implementation?

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 1
    Where were you seeing the duplication? (What's your source for the source of `IList`, so to speak?) – Jon Skeet Mar 20 '15 at 06:46
  • @JonSkeet: Just pressed `F12` / `Go to definition` on the `IList`. It showed me the `method signature` from the `assembly metadata`. Just viewed it from there. – now he who must not be named. Mar 20 '15 at 06:48
  • 1
    As an aside, it's helpful to only use backticks for things which are genuinely code. (I've edited the extra bits out of your post) – Jon Skeet Mar 20 '15 at 06:51
  • 2
    Just in case you were wondering, I've deleted my answer as I believe it to be incorrect - I suspect that the code really *was* like that at the point at which it was compiled. I can't really speculate why... personal preference of whoever wrote the interface, I guess. – Jon Skeet Mar 20 '15 at 06:56
  • See sources: [`IList`](http://referencesource.microsoft.com/mscorlib/a.html#b19f71a84062554b) does not include `IEnumerable` or `IEnumerable` in base interfaces list. Same goes for [`ICollection`](http://referencesource.microsoft.com/mscorlib/a.html#a9bf1395d3addc77), it does not include `IEnumerable`. – user4003407 Mar 20 '15 at 07:57

1 Answers1

10

Interface "inheritance" is one of the most misleading terms in software engineering. You didn't inherit squat, interfaces don't have any implementation so you can't inherit it either. You only inherit the demand to implement the methods.

Adding to that demand by repeating the interface declaration doesn't change anything, you already had the demand and adding an extra demand makes no difference whatsoever. So since it doesn't matter, Microsoft helpfully just repeats the interface so you can tell in one fell swoop what interfaces are implemented by, say, List. You don't have to drill down to the interface declaration to see that List implements IEnumerable as well. It is a self-documenting coding style, recommended.

Do beware of the other side of this medal, two distinct interfaces with the exact same method can be implemented with just a single method implementation. While that is often useful, sometimes that is not what you want. Say, ICowboy and IPainter, they both have a Draw() method. It should not do the same thing :) You then have to fall back to an explicit implementation to avoid the ambiguity.

Addressing the Resharper complaint, it isn't very helpful of course. Resharper tends to assume the worst from a programmer. But if you want to shut it up then you need to remove IOne from the IThree inheritance list, it is redundant. Same thing for the class that implements IThree, you'd also need to remove ITwo and IOne from the inheritance list. Or just turn off the warning.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536