Monday, April 25, 2011

Generic methods and multiple constraints

I have a generic method which has two generic parameters. I tried to compile the code below but it doesn't work. Is it a .NET limitation? Is it possible to have multiple constraints for different parameter?

public TResponse Call<TResponse, TRequest>(TRequest request) where TRequest : MyClass, TResponse : MyOtherClass

Thanks!

From stackoverflow
  • It is possible to do this, you've just got the syntax slightly wrong. You need a where for each constraint rather than separating them with a comma:

    public TResponse Call<TResponse, TRequest>(TRequest request)
        where TRequest : MyClass
        where TResponse : MyOtherClass
    

0 comments:

Post a Comment