Expression.Constant Method (Object, Type)

Creates a ConstantExpression that has the Value and Type properties set to the specified values.

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

Syntax

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

Parameters

Return Value

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

Exceptions

Exception Condition
ArgumentNullException

type is nulla null reference (Nothing in Visual Basic).

ArgumentException

value is not nulla null reference (Nothing in Visual Basic) and type is not assignable from the dynamic type of value.

Remarks

This method can be useful for representing values of nullable types.

Examples

The following code example shows how to create an expression that represents a constant of the nullable type and set its value to nulla null reference (Nothing in Visual Basic).

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

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

       // This code example produces the following output:
       //
       // null

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.