I want to pass a converter to a method, and the constraint must be that the converter always gets a string as argument. I tried the following, but it won’t compile:
class Test
{
public void Foo(string val, Converter<Tin,Tout> conv)
where Tin:string
{
myObj = conv(val);
}
}
Whenever a function signature should carry a generic argument, or an argument with a generic type parameter, that type parameter must be part of the method declaration and the method becomes generic itself.
Especially, the generic type arguments you want to constrain must be part of the method signature of Foo.
Try it this way: