Compartilhar via


Expression.Constant Método

Definição

Sobrecargas

Nome Description
Constant(Object)

Cria um ConstantExpression que tem a Value propriedade definida como o valor especificado.

Constant(Object, Type)

Cria um ConstantExpression que tem as propriedades e Type as Value propriedades definidas para os valores especificados.

Constant(Object)

Origem:
ConstantExpression.cs
Origem:
ConstantExpression.cs
Origem:
ConstantExpression.cs
Origem:
ConstantExpression.cs
Origem:
ConstantExpression.cs

Cria um ConstantExpression que tem a Value propriedade definida como o valor especificado.

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

Parâmetros

value
Object

Um Object para definir a Value propriedade igual a.

Retornos

Um ConstantExpression que tem a NodeType propriedade igual a Constant e a Value propriedade definida como o valor especificado.

Exemplos

O exemplo de código a seguir mostra como criar uma expressão que representa um valor constante.

// 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

Comentários

A Type propriedade do resultado ConstantExpression é igual ao tipo de value. Se value for null, Type é igual a Object.

Para representar null, você também pode usar o método, com o Constant(Object, Type) qual você pode especificar explicitamente o tipo.

Aplica-se a

Constant(Object, Type)

Origem:
ConstantExpression.cs
Origem:
ConstantExpression.cs
Origem:
ConstantExpression.cs
Origem:
ConstantExpression.cs
Origem:
ConstantExpression.cs

Cria um ConstantExpression que tem as propriedades e Type as Value propriedades definidas para os valores especificados.

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

Parâmetros

value
Object

Um Object para definir a Value propriedade igual a.

type
Type

A Type para definir a Type propriedade igual a.

Retornos

Um ConstantExpression que tem a NodeType propriedade igual a Constant e as Value propriedades e Type definidas para os valores especificados.

Exceções

type é null.

value não null é e type não é atribuível do tipo dinâmico de value.

Exemplos

O exemplo de código a seguir mostra como criar uma expressão que representa uma constante do tipo anulável e definir seu valor como 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

Comentários

Esse método pode ser útil para representar valores de tipos anuláveis.

Aplica-se a