Search code examples
c#genericsc#-4.0interfacetype-constraints

How to specify type constraint and inheritance on declaring class?


I've got an abstract class which has a type constraint. But i also want to make the abstract class implement an interface.

E.g:

public abstract class PostEvent<TPost> : IDomainEvent, where TPost : Post, new()

Which doesn't compile.

I don't want this:

public abstract class PostEvent<TPost> where TPost : Post, IDomainEvent, new()

Because that means TPost : IDomainEvent

I want PostEvent : IDomainEvent

What's the syntax?


Solution

  • Try this:

    public abstract class PostEvent<TPost> : IDomainEvent where TPost : Post, new() 
    

    You don't want a comma between the interface list and the generic constraints.