Expression.Parameter Method

Definition

Creates a ParameterExpression node that can be used to identify a parameter or a variable in an expression tree.

Overloads

Parameter(Type, String)

Creates a ParameterExpression node that can be used to identify a parameter or a variable in an expression tree.

Parameter(Type)

Creates a ParameterExpression node that can be used to identify a parameter or a variable in an expression tree.

Parameter(Type, String)

Source:
ParameterExpression.cs
Source:
ParameterExpression.cs
Source:
ParameterExpression.cs

Creates a ParameterExpression node that can be used to identify a parameter or a variable in an expression tree.

C#
public static System.Linq.Expressions.ParameterExpression Parameter(Type type, string name);
C#
public static System.Linq.Expressions.ParameterExpression Parameter(Type type, string? name);

Parameters

type
Type

The type of the parameter or variable.

name
String

The name of the parameter or variable, used for debugging or printing purpose only.

Returns

A ParameterExpression that has the NodeType property equal to Parameter and the Type and Name properties set to the specified values.

Exceptions

type is null.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Parameter(Type)

Source:
ParameterExpression.cs
Source:
ParameterExpression.cs
Source:
ParameterExpression.cs

Creates a ParameterExpression node that can be used to identify a parameter or a variable in an expression tree.

C#
public static System.Linq.Expressions.ParameterExpression Parameter(Type type);

Parameters

type
Type

The type of the parameter or variable.

Returns

A ParameterExpression node with the specified name and type.

Examples

The following example demonstrates how to create a MethodCallExpression object that prints the value of a ParameterExpression object.

C#
// Add the following directive to the file:
// using System.Linq.Expressions;

// Creating a parameter for the expression tree.
ParameterExpression param = Expression.Parameter(typeof(int));

// Creating an expression for the method call and specifying its parameter.
MethodCallExpression methodCall = Expression.Call(
    typeof(Console).GetMethod("WriteLine", new Type[] { typeof(int) }),
    param
);

// The following statement first creates an expression tree,
// then compiles it, and then runs it.
Expression.Lambda<Action<int>>(
    methodCall,
    new ParameterExpression[] { param }
).Compile()(10);

// This code example produces the following output:
//
// 10

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0