Expression.Constant 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
Constant(Object) |
建立 ConstantExpression,其 Value 屬性設定為指定的值。 |
Constant(Object, Type) |
建立 ConstantExpression,其 Value 和 Type 屬性設定為指定的值。 |
Constant(Object)
建立 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
參數
傳回
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
。 如果 為 value
null
, Type 則等於 Object。
若要表示 null
,您也可以使用 Constant(Object, Type) 方法來明確指定型別。
適用於
Constant(Object, Type)
建立 ConstantExpression,其 Value 和 Type 屬性設定為指定的值。
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
參數
傳回
ConstantExpression,其 NodeType 屬性等於 Constant,且 Value 和 Type 屬性設定為指定的值。
例外狀況
type
為 null
。
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 類型的值。