I have this simple piece of code:
class Program
{
static void test<T1>(Action<T1> action) { }
static void test<T1, T2>(Action<T1, T2> action) { }
static void B(int a) { }
static void C(string a, int b) { }
static void Main(string[] args)
{
test(B);
test(C);
}
}
which does not compile because of two errors :
The type arguments for method 'Program.test<T1>(Action<T1>)' cannot be
inferred from the usage. Try specifying the type arguments explicitly.
The type arguments for method 'Program.test<T1>(Action<T1>)' cannot be
inferred from the usage. Try specifying the type arguments explicitly.
Of course it works if I explicitely specify the type parameters.
test<int>(B);
or if I cast B to the proper Action type:
test((Action<int>) B)
I would rather have an elegant solution where the C# compiler identifies the correct method overload automatically.
Is this only possible?