views:

421

answers:

1

I see stuff like this a lot:

interface A { ... }
interface B : A { ... }
class C : B, A { ...}

Why would you specify that C implements interface A, when B already inherits A? Does it make any semantic difference or is it just a matter of style?

(One of many examples is List<T> implementing IList<T> and ICollection<T>, while IList<T> also derives from ICollection<T>).


Update: Thanks for confirming my guess that it doesn't make any semantic difference.

I have come up with a related situation where it does make a difference to explicitly name an interface that is already in the inheritance tree:

If B were a class, C would only (re-)implement interface members from A if it names A explicitly after the ':'.

[EDIT] I changed the wording of the question to avoid confusion with explicitly implemented interface members, which restrict the use of the member to cases where the object is cast as the interface.

+7  A: 

I believe this is just a matter of style. It is specifically important when looking at framework/library classes - in your example, for instance, it highlights the idea that this class can be treated as either an ICollection or an IList, without the developer having to know that IList is actually an ICollection.

It has no functional ramifications. Specifically, this code would compile whether or not class 'C' implements 'A' explicitly:

namespace DotNetInterfaceTest {
    class Program {
     static void Main(string[] args) {
      A c = new C();
     }
    }

    interface A {
     void foo();
    }

    interface B : A {
     void bar();
    }

    class C : B {
     public void bar() {}
     public void foo() {}
    }
}
Hershi
I think it's for better readability.. without having to work your brain cells traversing the interface inheritance hierarchy.
Gishu