Why are parentheses needed around this lambda function?

165 Views Asked by At
return (Func<object, Task<object>>)(async (dynamic data) =>
{
    methodCall(data.data, data.more);
    return null;
});

From what I gather the above code is doing, it is explicitly casting the lambda function as a delegate, and then returning that delegate (in this case, it is returning to an edgejs function).

When the parens are removed, 8 errors are received from the build process.

Syntax error, ',' expected

; expected

} expected

The name async does not exist in the current context

The name dynamic does not exist in the current context

The name data does not exist in the current context

The name data does not exist in the current context

The name data does not exist in the current context

1

There are 1 best solutions below

3
On BEST ANSWER

Likely, it's because async is a contextual keyword that only has special meaning when appearing as a modifier in a method or lambda signature. Without the parens, the parser thinks async is a method name, resulting in an error because the compiler cannot find a method with that name (and, further, a parse error on the => because the parser is no longer consuming a lambda expression at that point).