user20051042
user20051042

Reputation: 37

cannot be inferred from the usage. Try specifying the type arguments explicitly from iinterface

beloww is my interface

public interface IMyInterface
  {
   public Task<IApiResponseItem<T>> MyMethodt<T>(string inp1, string inp2,
  string inp3="SqlKey", ApiMethod method = ApiMethod.Get,
  TimeSpan? timeout = null) where T : new();
  }

and implementation

 public async Task<IApiResponseItem<T>> MyMethodt<T>(string inp1, string inp2,
  string inp3="SqlKey", ApiMethod method = ApiMethod.Get,
  TimeSpan? timeout = null) where T : new();
  {
  
  }

below is the call

var iApiResponse = await _resilentApi.MyMethodt(inp1, inp2, "SqlKey", ApiMethod.Get, timeout);  

but, i always receiving error as

The type arguments for method 'IMyInterface.MyMethodt(string, string, string, ApiMethod, TimeSpan?)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Upvotes: 1

Views: 2932

Answers (1)

Thomas Weller
Thomas Weller

Reputation: 59503

You pass in a string, a string, a string, an enum and a TimeSpan to the method. You never specified T in the parameters of the method. How would the compiler be able to figure out what T is? It could be anything.

There are 2 possible solutions.

  1. you need to specify the type as part of the method call.

    var iApiResponse = await _resilentApi.MyMethodt<Whatever>(inp1, inp2, "SqlKey", ApiMethod.Get, timeout); 
                                                    ^^^^^^^^
    
  2. make T part of the method signature somewhere

    public async Task<IApiResponseItem<T>> MyMethodt<T>(string inp1, string inp2, T t,
         string inp3 = "SqlKey", ApiMethod method = ApiMethod.Get,                ^^^
         TimeSpan? timeout = null) where T : new()
    

Upvotes: 0

Related Questions