Επεξεργασία

Κοινή χρήση μέσω


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.

public:
 static System::Linq::Expressions::ConstantExpression ^ Constant(System::Object ^ value);
public static System.Linq.Expressions.ConstantExpression Constant (object value);
public static System.Linq.Expressions.ConstantExpression Constant (object? value);
static member Constant : obj -> System.Linq.Expressions.ConstantExpression
Public Shared Function Constant (value As Object) As ConstantExpression

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.

// 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
' Add the following directive to your file:
' Imports System.Linq.Expressions 

' This expression represents a constant value.
Dim constantExpr As Expression = Expression.Constant(5.5)

' Print the expression.
Console.WriteLine(constantExpr.ToString())

' You can also use variables.
Dim num As Double = 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

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.

public:
 static System::Linq::Expressions::ConstantExpression ^ Constant(System::Object ^ value, Type ^ type);
public static System.Linq.Expressions.ConstantExpression Constant (object value, Type type);
public static System.Linq.Expressions.ConstantExpression Constant (object? value, Type type);
static member Constant : obj * Type -> System.Linq.Expressions.ConstantExpression
Public Shared Function Constant (value As Object, type As Type) As ConstantExpression

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.

// 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
' Add the following directive to your file:
' Imports 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.
Dim constantExpr As Expression = Expression.Constant(
                            Nothing,
                            GetType(Double?)
                        )

' Print 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