An interface that "extends itself" is ensuring what?

Viewed 92

I was following a tutorial for an EventBus in and came across the following Interface:

public interface IEventHandler<in TEvent> : IEventHandler where TEvent : Event
{
    Task Handle(TEvent @event);
}

public interface IEventHandler
{

}

Now, I get the generics part and the restriction, but I'm missing the point where the Interface extends itself. It's the first time I've seen it being used like this. What does it accomplish? Can anybody please shed some light on this subject? Thanks!

1 Answers

In C# the types Interface and Interface<T> (you will sometimes see this as Interface`1) are distinct types. They have no relation inside the type system (unless one interface extends the other).

This can be confusing when you come from a Java background, where you have "raw types" and a List<T> is always a List at the same time. This is not true for C#.

You could rename the interfaces to make this distinction clearer:

IEventHandler<in TEvent> : IEventHandlerRaw

(this is only to make the explanation clearer. It is not recommended in production code)

Related