Waddaulookingat
Waddaulookingat

Reputation: 387

how to declare a generic method with a different "generic" return type

I have a interface that looks like this

public interface IBlueFinApiClient
{
    Task<BlueFinApiResponse> DecryptAsync(BlueFinApiRequest request);
}

This method, when implemented, just goes to an endpoint with the request and posts in. I need to use this end point to post another request of a different type and the return from Task<> will be of a different type as well. So, I am trying to change this into a generic but I am kind stuck on how to define the return type in a generic format:

public interface IBlueFinApiClient<T> where T:class
{
    Task<What should happen here?> DecryptAsync(T request) 

I was kinda exploring the covariance route but I am not sure if that's the answer here.

Upvotes: 2

Views: 54

Answers (1)

DavidG
DavidG

Reputation: 118947

You can specify multiple generic type parameters, for example:

public interface IBlueFinApiClient<TInput, TReturn>
{
    Task<TReturn> DecryptAsync(TInput request);
}

Alternatively, you could specify the return type as a generic method:

public interface IBlueFinApiClient<TInput>
{
    Task<TReturn> DecryptAsync<TReturn>(TInput request);
}

Upvotes: 4

Related Questions