Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
There are several errors related to declaring and using lambda expressions:
- CS0748: Inconsistent lambda parameter usage; parameter types must be all explicit or all implicit.
- CS1621: The yield statement cannot be used inside an anonymous method or lambda expression.
- CS1628: Cannot use
inreforoutparameter inside an anonymous method, lambda expression, or query expression. - CS1632: Control cannot leave the body of an anonymous method or lambda expression.
- CS1673: Anonymous methods, lambda expressions, and query expressions inside structs cannot access instance members of 'this'.
- CS1686: Local variable or its members cannot have their address taken and be used inside an anonymous method or lambda expression.
- CS1706: Expression cannot contain anonymous methods or lambda expressions.
- CS8030: Anonymous function converted to a void returning delegate cannot return a value.
- CS8175: Cannot use ref local inside an anonymous method, lambda expression, or query expression.
- CS8916: Attributes on lambda expressions require a parenthesized parameter list.
- CS8971: InterpolatedStringHandlerArgument has no effect when applied to lambda parameters and will be ignored at the call site.
- CS8975: The contextual keyword
varcannot be used as an explicit lambda return type. - CS9098: Implicitly typed lambda parameter '...' cannot have a default value.
In addition, there are several warnings related to declaring and using lambda expressions:
- CS8971: InterpolatedStringHandlerArgument has no effect when applied to lambda parameters and will be ignored at the call site.
- CS9099: The default parameter value does not match in the target delegate type.
- CS9100: Parameter has params modifier in lambda but not in target delegate type.
The compiler also produces the following informational message:
- CS9236: Compiling requires binding the lambda expression at least count times. Consider declaring the lambda expression with explicit parameter types, or if the containing method call is generic, consider using explicit type arguments.
Syntax limitations in lambda expressions
Some C# syntax is prohibited in lambda expressions and anonymous methods. Using invalid constructs in a lambda expression causes the following errors:
- CS1621: The
yieldstatement cannot be used inside an anonymous method or lambda expression. - CS1628: Cannot use
in,ref, oroutparameter inside an anonymous method, lambda expression, or query expression. - CS1632: Control cannot leave the body of an anonymous method or lambda expression.
- CS1673: Anonymous methods, lambda expressions, and query expressions inside structs cannot access instance members of
this. - CS1686: Local variable or its members cannot have their address taken and be used inside an anonymous method or lambda expression.
- CS8175: Cannot use ref local inside an anonymous method, lambda expression, or query expression.
All the following constructs are disallowed in lambda expressions:
yieldstatements (yield returnoryield break)- Calling a method that has an
in,ref, oroutparameter reflocal variablesbreak,goto, andcontinuestatementsthisaccess whenthisis astructtype- Anonymous methods or lambda expressions inside another expression, such as an Attribute constructor.
You can't use any of these constructs in a lambda expression or an anonymous method. Many are allowed in a local function.
In addition, interpolated string handler types are ignored when applied to a lambda parameter. If you use one, you see the following warning:
- CS8971: InterpolatedStringHandlerArgument has no effect when applied to lambda parameters and will be ignored at the call site.
Certain expressions cause the compiler to emit the following informational warning:
- CS9236: Compiling requires binding the lambda expression at least count times. Consider declaring the lambda expression with explicit parameter types, or if the containing method call is generic, consider using explicit type arguments.
The complexity of the lambda expressions and how they invoke other lambda expressions is negatively impacting compiler performance. The reason is that the compiler must infer parameter and argument types through the lambda expressions and the potential types takes time.
Lambda expression parameters and returns
These errors indicate a problem with a parameter declaration:
- CS0748: Inconsistent lambda parameter usage; parameter types must be all explicit or all implicit.
- CS9098: Implicitly typed lambda parameter '...' cannot have a default value.
- CS8030: Anonymous function converted to a void returning delegate cannot return a value.
- CS8916: Attributes on lambda expressions require a parenthesized parameter list.
- CS8975: The contextual keyword 'var' cannot be used as an explicit lambda return type.
Lambda expression parameters must follow these rules:
- When a lambda expression has multiple parameters, either all parameters must be explicitly typed or all parameters must be implicitly typed.
- All lambda parameters with a default value must be explicitly typed.
- If attributes are applied to any parameters, the parameter list must be enclosed in parentheses.
Return types of lambda expression must follow these rules:
- A lambda expression that returns any value can't be converted to a
voidreturning delegate, such asAction. - The return type is either inferred, or an explicit type. A return type declared using the keyword
varisn't allowed.
Lambda expression delegate type
- CS9099: Warning: The default parameter value does not match in the target delegate type.
When you declare a default value or add the params modifier with a lambda expression parameter, the delegate type isn't one of the Func or Action types. Rather, it's a custom type that includes the default parameter value or params modifier. The following code generates warnings because it assigns a lambda expression that has a default parameter to an Action type:
Action<int> a1 = (int i = 2) => { };
Action<string[]> a3 = (params string[] s) => { };
To fix the error, either remove the default parameter or use an implicitly typed variable for the delegate type:
Action<int> a1 = (int i) => { };
var a2 = (int i = 2) => { };
Action<string[]> a3 = (string[] s) => { };
var a4 = (params string[] s) => { };