When I run this program I get two errors on the line containing fn(3,4)
Argument 1: cannot convert from 'int' to T
Argument 2: cannot convert from 'int' to T
I thought type T would be int, as specified by the lambda. But if so then why the conversion errors? Am I misunderstanding something?
class Program
{
static void DoStuff<T>(Func<T, T, T> fn)
{
Console.WriteLine(fn(3, 4));
}
static void Main()
{
DoStuff((int x, int y) => x + y);
}
}
This works, if I change the parameters to accept the ints as arguments:
class Program
{
static void DoStuff<T>(T x, T y, Func<T, T, T> fn)
{
Console.WriteLine(fn(x, y));
}
static void Main()
{
DoStuff(3, 4, (int x, int y) => x + y);
}
}
I come from a C++ background, so trying to get my head around what works and what doesn't in C#