Freigeben über


Expression.Constant Method (Object)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Creates a ConstantExpression that has the Value property set to the specified value.

Namespace:  System.Linq.Expressions
Assembly:  System.Core (in System.Core.dll)

Syntax

'Declaration
Public Shared Function Constant ( _
    value As Object _
) As ConstantExpression
public static ConstantExpression Constant(
    Object value
)

Parameters

Return Value

Type: System.Linq.Expressions.ConstantExpression
A ConstantExpression that has the NodeType property equal to Constant and the Value property set to the specified value.

Remarks

The Type property of the resulting ConstantExpression is equal to the type of value. If value is nulla null reference (Nothing in Visual Basic), Type is equal to Object.

To represent nulla null reference (Nothing in Visual Basic), you can also use the Constant(Object, Type) method, with which you can explicitly specify the type.

Examples

The following code example shows how to create an expression that represents a constant value.

' 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.
outputBlock.Text &= constantExpr.ToString() & vbCrLf

' You can also use variables.
Dim num As Double = 3.5
constantExpr = Expression.Constant(num)
outputBlock.Text &= constantExpr.ToString() & vbCrLf

' This code example produces the following output:
'
' 5.5
' 3.5
// 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.
outputBlock.Text += constantExpr.ToString() + "\n";

// You can also use variables.
double num = 3.5;
constantExpr = Expression.Constant(num);
outputBlock.Text += constantExpr.ToString() + "\n";

// This code example produces the following output:
//
// 5.5
// 3.5

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.