Use interface inheritance or just implement all interfaces directly?

I do not necessarily see the enormous advantage of interfaces inheriting interfaces.

Consider the following three interfaces: a third, inheriting the other two

interface IOne { } interface ITwo { } // interface inheritance interface IAll : IOne, ITwo { } 

Is it better

 class C : IOne, ITwo { ... } 

or

 class C : IAll { ... } 

If the latter can be useful, then why not just create an IAll to have all the methods of both IOne and ITwo without inheriting from them? In the end it is an interface.

Is interface inheritance practical, not just useful?

+1
inheritance c # interface
source share
5 answers

A good way to think about interface inheritance is to completely forget what it is called "inheritance." In the end, it is obvious that the "base" interface does not introduce any functionality into the "derived" interface; all that he makes is a promise that there will be certain members.

Derived class relationships usually imply an is-a-kind-of relationship. The NullReferenceException is an exception. The interface output relationship relation does not mean at all that it makes no sense to say that IEnumerable<T> is a kind of IDisposable .

Rather, interface inheritance means that "a type that implements IEnumerable<T> must also implement IDisposable . That is, interface inheritance does not mean" it's kind of ", but rather," every implementation of this also needs to implement this. "

Does it make it more attractive?

+6
source share

Do what is semantically correct. Do nothing to save input in the secondary interface.

If "IAll" is a concept that makes sense in your program, well, use it. If this is just a way not to enter "IFirst, ISecond", do not.

+2
source share

There may be methods that need an IAll argument, so if you meet the requirements for implementing the entire IAll , it is convenient to do this, and not just implement each of the interfaces that it extends!

Usually, of course, the interfaces are not empty, so there is a certain (maybe small) cost when implementing one and / or the other (you need to implement various, several methods). If, say, IFirst has a foo method, and ISecond has a bar method, it makes sense to extend both to IBoth , even if this does not add additional necessary methods - this allows you to clearly express that it needs an argument that has both methods , foo and bar .

If every interface in the world was empty, their use (a much smaller extension of them using an empty interface) would be much more doubtful, of course! -)

+1
source share

Yes, interface inheritance is practical. The idea of ​​interfaces is that one interface defines a free "contract" that describes a specific set of functionality. This allows a very simple separation of functionality.

Typically, a class that must implement several interfaces does this by listing each of the interfaces separately, for example:

class C : IOne, ITwo { ... }

If you need the ability to aggregate functionality from multiple interfaces, you need to create one interface that inherits from others (for example, your IAll interface). Although you can do it, you don’t need to (or want to) do it at all.

+1
source share

I would go with the first approach, it is much clearer for me. The second seems far-fetched, IMO.

0
source share

All Articles