A statement lambda is a lambda expression in C# where the execution body is enclosed in a statement blockDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
{ } containing one or more statements. Unlike expression lambdas, which evaluate and implicitly return a single expression, statement lambdas permit complex control flow, local variable declarations, and require an explicit return statement if the delegate signature expects a non-void return type.
Syntax
Technical Characteristics
- Delegate Resolution: A statement lambda must be convertible to a compatible delegate type, typically from the
Actionfamily (forvoidreturns) or theFuncfamily (for value returns). - Return Semantics: If the target delegate type has a non-void return type, the statement block must contain an explicit
returnstatement on all reachable code paths. - Expression Tree Incompatibility: Unlike expression lambdas, statement lambdas cannot be converted into expression trees (
System.Linq.Expressions.Expression<TDelegate>). The C# compiler can only emit them as executable Intermediate Language (IL) bound to a delegate instance. - Lexical Scoping and Closures: The statement block defines a local scope. It can capture variables from the enclosing method (forming a closure), but variables declared within the
{ }block are strictly local to the lambda’s invocation. - Asynchronous Execution: Statement lambdas can be modified with the
asynckeyword, allowing the use ofawaitwithin the statement block. This resolves to anAction(if returningvoid),Func<Task>, orFunc<Task<T>>.
Syntax Visualization
Void-returning Statement Lambda (Action)
Func)
Master C# with Deep Grasping Methodology!Learn More





