Expression.Constant 메서드

정의

ConstantExpression을 만듭니다.

오버로드

Constant(Object)

ConstantExpression 속성이 지정된 값으로 설정된 Value을 만듭니다.

Constant(Object, Type)

ConstantExpressionValue 속성이 지정된 값으로 설정된 Type을 만듭니다.

Constant(Object)

Source:
ConstantExpression.cs
Source:
ConstantExpression.cs
Source:
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과 같습니다. 가 이nullvalueType 와 같습니다Object.

을 나타내 null려면 메서드를 사용하여 형식을 Constant(Object, Type) 명시적으로 지정할 수도 있습니다.

적용 대상

Constant(Object, Type)

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

ConstantExpressionValue 속성이 지정된 값으로 설정된 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

매개 변수

value
Object

Object 속성에 설정할 Value입니다.

type
Type

Type 속성에 설정할 Type입니다.

반환

ConstantExpression 속성이 NodeType이고 ConstantValue 속성이 지정된 값으로 설정된 Type입니다.

예외

type이(가) null인 경우

valuenull이 아니고 type의 동적 형식에서 value을 할당할 수 없는 경우

예제

다음 코드 예제에서는 nullable 형식의 상수를 나타내는 식을 만들고 해당 값을 로 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

설명

이 메서드는 nullable 형식의 값을 나타내는 데 유용할 수 있습니다.

적용 대상