Expression.Constant Method

Definition

Creates a ConstantExpression.

Overloads

Constant(Object)

Creates a ConstantExpression that has the Value property set to the specified value.

Constant(Object, Type)

Creates a ConstantExpression that has the Value and Type properties set to the specified values.

Constant(Object)

Source:
ConstantExpression.cs
Source:
ConstantExpression.cs
Source:
ConstantExpression.cs

Creates a ConstantExpression that has the Value property set to the specified value.

C#
public static System.Linq.Expressions.ConstantExpression Constant(object value);
C#
public static System.Linq.Expressions.ConstantExpression Constant(object? value);

Parameters

value
Object

An Object to set the Value property equal to.

Returns

A ConstantExpression that has the NodeType property equal to Constant and the Value property set to the specified value.

Examples

The following code example shows how to create an expression that represents a constant value.

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

// This expression represents a Constant value.
Expression constantExpr = Expression.Constant(5.5);

// Print out the expression.
Console.WriteLine(constantExpr.ToString());

// You can also use variables.
double num = 3.5;
constantExpr = Expression.Constant(num);
Console.WriteLine(constantExpr.ToString());

// This code example produces the following output:
//
// 5.5
// 3.5

Remarks

The Type property of the resulting ConstantExpression is equal to the type of value. If value is null, Type is equal to Object.

To represent null, you can also use the Constant(Object, Type) method, with which you can explicitly specify the type.

Applies to

.NET 10 och andra versioner
Produkt Versioner
.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

Constant(Object, Type)

Source:
ConstantExpression.cs
Source:
ConstantExpression.cs
Source:
ConstantExpression.cs

Creates a ConstantExpression that has the Value and Type properties set to the specified values.

C#
public static System.Linq.Expressions.ConstantExpression Constant(object value, Type type);
C#
public static System.Linq.Expressions.ConstantExpression Constant(object? value, Type type);

Parameters

value
Object

An Object to set the Value property equal to.

type
Type

A Type to set the Type property equal to.

Returns

A ConstantExpression that has the NodeType property equal to Constant and the Value and Type properties set to the specified values.

Exceptions

type is null.

value is not null and type is not assignable from the dynamic type of value.

Examples

The following code example shows how to create an expression that represents a constant of the nullable type and set its value to null.

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

// This expression represents a constant value,
// for which you can explicitly specify the type.
// This can be used, for example, for defining constants of a nullable type.
Expression constantExpr = Expression.Constant(
                            null,
                            typeof(double?)
                        );

// Print out the expression.
Console.WriteLine(constantExpr.ToString());

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

Remarks

This method can be useful for representing values of nullable types.

Applies to

.NET 10 och andra versioner
Produkt Versioner
.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