public delegate T GenDel<T>();
class Program
{
public static void genMet<T>(GenDel<T> d) { }
static void Main(string[] args)
{
genMet(new GenDel<string>(() => "Works"));
genMet(() => "Works");
}
}
In above example the generic method receives lambda expression as a parameter ( genMet(() => "Works");
), and from this lambda expression method is able to infer parameter types.
Why isn't method also able to infer parameter type in the next example, where instead of lambda expression we pass a delegate instance as a parameter:
genMet(new GenDel(() => "Doesn't work")); // Error: Using the generic type 'GenDel<T>'
// requires 1 type arguments
Type inference only applies to generic methods, not generic types or their constructors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With