Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement both generic and non-generic version of a class?

Tags:

c#

generics

I want to implement a non-generic version of my generic class. Like this.

public class ServerSentEvent : ServerSentEvent<NoAdditionalClientInformation>

public class ServerSentEvent<ClientInfo> : IServerSentEvent

To solve this I had to make a dummy/empty class - NoAdditionalClientInformation.

Is there another way to do this without the empty class?

like image 938
Erik Z Avatar asked Jan 16 '13 07:01

Erik Z


1 Answers

Usually you’d just do it the other way around:

public class ServerSentEvent : IServerSentEvent
{}

public class ServerSentEvent<ClientInfo> : ServerSentEvent
{}

That way the generic version is a more specified subtype of the non-generic one allowing you to put more information in it but to use the generic type whereever a non-generic type is expected.

If you do it like you suggested, you would need to have to specify some default type; if you can’t think of a default one, it is probably the wrong order, but in general it might depend on the case.

like image 97
poke Avatar answered Sep 27 '22 22:09

poke