ParameterExpression Class

Namespace: System.Linq.Expressions

Represents a parameter in a LambdaExpression. This class is used to define input parameters for expression trees.

Syntax

public sealed class ParameterExpression : Expression

Remarks

A ParameterExpression represents a named parameter. It is immutable and can be reused in multiple expression trees. Use the static factory methods on the Expression class, such as Expression.Parameter, to create instances of ParameterExpression.

Properties

  • Name string Name { get; }

    Gets the name of the parameter.

  • IsByRef bool IsByRef { get; }

    Gets a value that indicates whether the parameter is passed by reference.

  • Type Type Type { get; }

    Gets the static type of the expression that is the value of the expression.

  • NodeType ExpressionType NodeType { get; }

    Gets the ExpressionType of the provider expression.

  • CanReduce bool CanReduce { get; }

    Gets a value that indicates whether the expression can be reduced.

Methods

  • Update ParameterExpression Update(Type type, string name)

    Replaces the children of the Expression with the nodes from the specified collection.

  • Reduce Expression Reduce()

    Causes the expression to be reduced to its simplest form.

  • Accept TResult Accept<TResult>(ExpressionVisitor<TResult> visitor)

    Dispatches to the specific visit method for this abstract syntax tree node. For example, ParameterExpression, which inherits from Expression, calls VisitParameter.

Example


using System;
using System.Linq.Expressions;

public class Example
{
    public static void Main(string[] args)
    {
        // Create a parameter expression for an integer parameter named "x"
        ParameterExpression parameterX = Expression.Parameter(typeof(int), "x");

        // Create a parameter expression for a string parameter named "message"
        ParameterExpression parameterMessage = Expression.Parameter(typeof(string), "message");

        // Create a lambda expression that takes two integer parameters and returns their sum
        ParameterExpression paramA = Expression.Parameter(typeof(int), "a");
        ParameterExpression paramB = Expression.Parameter(typeof(int), "b");
        var sum = Expression.Lambda(
            Expression.Add(paramA, paramB),
            paramA, paramB
        );

        Console.WriteLine($"Parameter X: Name = {parameterX.Name}, Type = {parameterX.Type}");
        Console.WriteLine($"Parameter Message: Name = {parameterMessage.Name}, Type = {parameterMessage.Type}");
        Console.WriteLine($"Lambda Expression: {sum.Body}");
    }
}