System.Linq.Expressions.LambdaExpression Class

Class LambdaExpression

Represents a lambda expression, which is an anonymous function that can contain expressions and statements. It is the base class for Expression<TDelegate>.

Namespace: System.Linq.Expressions

Assembly: System.Linq.Expressions.dll

Inheritance: Expression

Syntax

public abstract class LambdaExpression : Expression

Note:LambdaExpression is an abstract base class. You will typically work with the derived type Expression<TDelegate>, which provides type safety for the delegate signature.

Properties

Name Description
Body Gets the body of the lambda expression. This is the Expression that represents the code to be executed.
CanReduce When overridden in a derived class, gets a value that indicates whether the expression tree can be reduced to a simpler expression.
Name Gets the name of the lambda expression.
NodeType Gets the ExpressionType that represents the type of this expression. For LambdaExpression, this is ExpressionType.Lambda.
Parameters Gets the parameters of the lambda expression.
Type Gets the static type of the expression that represents the lambda expression. This will be a delegate type.

Methods

Name Description
Compile() Compiles the lambda expression into an executable delegate.
Compile(Type delegateType) Compiles the lambda expression into an executable delegate of the specified type.
Reduce() Reduces the expression tree to a simpler expression.
ToString() Returns a string representation of the lambda expression.

Examples

Creating a Lambda Expression

The following example shows how to create a lambda expression that adds two numbers using Expression.Lambda.


using System;
using System.Linq.Expressions;

public static class LambdaExpressionExample
{
    public static void Main()
    {
        // Define parameters
        ParameterExpression param1 = Expression.Parameter(typeof(int), "x");
        ParameterExpression param2 = Expression.Parameter(typeof(int), "y");

        // Create the body of the lambda expression (x + y)
        BinaryExpression body = Expression.Add(param1, param2);

        // Create the lambda expression
        LambdaExpression lambda = Expression.Lambda(body, param1, param2);

        // Print the lambda expression to the console
        Console.WriteLine("Lambda Expression: " + lambda.ToString());

        // Compile the lambda expression to a delegate
        Func<int, int, int> compiledLambda = (Func<int, int, int>)lambda.Compile();

        // Execute the compiled delegate
        int result = compiledLambda(5, 3);
        Console.WriteLine("Result of execution: " + result);

        // Example with Expression<TDelegate> for type safety
        Expression<Func<int, int, int>> typedLambda =
            Expression.Lambda<Func<int, int, int>>(
                Expression.Add(
                    Expression.Parameter(typeof(int), "a"),
                    Expression.Parameter(typeof(int), "b")
                ),
                new ParameterExpression[] {
                    Expression.Parameter(typeof(int), "a"),
                    Expression.Parameter(typeof(int), "b")
                }
            );

        Console.WriteLine("\nTyped Lambda Expression: " + typedLambda.ToString());
        Func<int, int, int> typedCompiledLambda = typedLambda.Compile();
        int typedResult = typedCompiledLambda(10, 7);
        Console.WriteLine("Result of typed execution: " + typedResult);
    }
}

Remarks

Lambda expressions are a fundamental part of LINQ and are used extensively for writing query expressions and defining functional operations. The LambdaExpression class provides the core functionality for representing these expressions in an Expression<TDelegate> tree.

When you compile a LambdaExpression, an executable delegate is generated. This delegate can then be invoked like any other C# delegate, allowing you to execute the logic defined within the lambda expression.

Tip: For type-safe lambda expressions, always prefer using Expression<TDelegate> over directly instantiating or manipulating LambdaExpression, as it provides compile-time checking of delegate signatures.

Related Topics