I have code:
public delegate int SomeDelegate(int p);
public static int Inc(int p) {
return p + 1;
}
I can cast Inc
to
SomeDelegate a = Inc;
Func b = Inc;
is short for
SomeDelegate a = new SomeDelegate(Inc); // no cast here
Func b = new Func(Inc);
You can't cast an instance of SomeDelegate to a Func
This works:
Func c = x => a(x);
which is syntactic sugar for
class MyLambda
{
SomeDelegate a;
public MyLambda(SomeDelegate a) { this.a = a; }
public int Invoke(int x) { return this.a(x); }
}
Func c = new Func(new MyLambda(a).Invoke);