Jorn Theunissen Jorn Theunissen - 11 months ago 35
C# Question

Generic method returning generic type

I am using a restclient that accepts a type, which is later converted to the correct


IRestResponse<MyClassA> response = client.Execute<MyClassA>();

can also be
I thought about making a generic method which could handle this. However without any luck. This is my attempt:

IRestResponse<MyClassA> response = PerformExecuteClient<MyClassA>()

private RestResponse<T> PerformExecuteClient<T>() {
return client.Execute<T>();

The compiler tells me that
does not accept abstract types. Which makes sense, but I have no idea how else to make this method. Is it possible what I am trying to achieve here?

Additional information based on some comments. As Brains Mains suggested, the method is not accepting the abstract type here.

IRestResponse<T> Execute<T>(IRestRequest request) where T : new();


The signature is:

IRestResponse<T> Execute<T>(IRestRequest request) where T : new();

The generic constraint of new does not allow abstract classes. Instead you can:

  1. Have an interface for your classes and have a generic constraint on that.
  2. Change your MyClassA to not being abstract
  3. Remove the constraint

Me personally - I'd say the interface option - but this is opinion based :)

In any case for the PerformExecuteClient<T> it must follow at least the generic constraints of the Execute<T> which it executes.