c# – Why does a class implement an interface that is inherited by another interface of that class?

Question:

Looking through the AutoMapper source, I came across an interesting thing:

Mapper class:

public class Mapper : IRuntimeMapper, IMapper
{
//...

IRuntimeMapper interface:

public interface IRuntimeMapper : IMapper
{
//...

Question

Why IMapper Mapper implement IMapper if IRuntimeMapper already inherits from it? Did this "error" occur while expanding the library, or is it normal practice?

Answer:

In the source code of the auto-mapper on the github there are lines

public class Mapper : IRuntimeMapper, IMapper

no, and never was, judging by the logs.

Decompilers can show the entire chain of inheritance of interfaces for a rather strange reason – inheritance of classes and interfaces in C # works differently.

In the case of a class hierarchy, each descendant has exactly one explicit parent:

class A {}

class B : A {} 

class C : B {}

In the case of an interface, the hierarchy collapse mechanism is triggered:

A class or struct that directly implements an interface also directly implements all of the interface's base interfaces implicitly. This is true even if the class or struct doesn't explicitly list all base interfaces in the base class list.

The compiler looks for all base interfaces, builds a complete list from them, and adds them as being implemented. So he turns

interface A { }

interface B : A { }

public class SomeClass : B { }

v

public class SomeClass : B, A { }

It is in this form that the list of inherited interfaces is stored in the metadata, and decompilers simply cannot figure out whether there was a mention of two interfaces in the original code, or the compiler added them.

Scroll to Top