I’m confused as to why this works:
Action myAction1 = () => myIntFunc();
...
private Int32 myIntFunc() {
return(4);
}
I would expect the compiler to not allow this because an Action represents functions that do not return parameters, yet myIntFunc clearly returns a parameter.
As further evidence, note that this does not work:
Action myAction2 = myIntFunc;
So it’s like the lambda syntax is letting me get away with something I shouldn’t be able to get away with. I’m guessing I’m overlooking something to do with how Actions and lambdas work together…??
In the C# language specification, paragraph 6.5, one of the bullets says: If D has a void return type and the body of F is an expression, when each parameter of F is given the type of the corresponding parameter in D, the body of F is a valid expression (wrt §7) that would be permitted as a statement-expression (§8.6).
So it’s OK to throw away the return value in that case.
For a method group conversion, see paragraph 6.6 instead. Then the method must be compatible including the return type.
So there’s a difference between anonymous method (lambda) and normal (named) method (or method group).