Constant Method (Object)
Collapse the table of content
Expand the table of content

Expression.Constant Method (Object)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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

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

public static ConstantExpression Constant(
	Object value
)

Parameters

value
Type: System.Object
An Object to set the Value property equal to.

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.

The Type property of the resulting ConstantExpression is equal to the type of value. If value is null, Type is equal to Object.

To represent null, you can also use the Constant(Object, Type) method, with which you can explicitly specify the type.

The following code example shows how to create an expression that represents a 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.
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


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft