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

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)

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.

Exception Condition
ArgumentNullException

type is null.

ArgumentException

value is not null 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 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

Universal Windows Platform
Available since 8
.NET Framework
Available since 3.5
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show:
© 2016 Microsoft