共用方式為


Expression.Constant 方法

定義

建立 ConstantExpression

多載

Constant(Object)

建立 ConstantExpression,其 Value 屬性設定為指定的值。

Constant(Object, Type)

建立 ConstantExpression,其 ValueType 屬性設定為指定的值。

Constant(Object)

來源:
ConstantExpression.cs
來源:
ConstantExpression.cs
來源:
ConstantExpression.cs

建立 ConstantExpression,其 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

參數

value
Object

要將 Object 屬性設定為與之相等的 Value

傳回

ConstantExpression,其 NodeType 屬性等於 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

備註

Type產生的 ConstantExpression 屬性等於 的型別value。 如果 為 valuenullType 則等於 Object

若要表示 null,您也可以使用 Constant(Object, Type) 方法來明確指定型別。

適用於

Constant(Object, Type)

來源:
ConstantExpression.cs
來源:
ConstantExpression.cs
來源:
ConstantExpression.cs

建立 ConstantExpression,其 ValueType 屬性設定為指定的值。

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

參數

value
Object

要將 Object 屬性設定為與之相等的 Value

type
Type

要將 Type 屬性設定為與之相等的 Type

傳回

ConstantExpression,其 NodeType 屬性等於 Constant,且 ValueType 屬性設定為指定的值。

例外狀況

typenull

value 不為 null,且 type 無法從 value 的動態類型指派。

範例

下列程式代碼範例示範如何建立表達式,代表可為 Null 類型的常數,並將其值設定為 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

備註

這個方法可用於表示可為 Null 類型的值。

適用於