Export (0) Print
Expand All

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)

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

Parameters

value
Type: System.Object

An Object to set the Value property equal to.

type
Type: System.Type

A Type to set the Type property equal to.

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.

ExceptionCondition
ArgumentNullException

type is Nothing.

ArgumentException

value is not Nothing and type is not assignable from the dynamic type of value.

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

The following code example shows how to create an expression that represents a constant of the nullable type and set its value to Nothing.

' 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

.NET Framework

Supported in: 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft