Expression.Constant Method (Object, Type)
.NET Framework 4.5
Creates a ConstantExpression that has the Value and Type properties set to the specified values.
Assembly: System.Core (in System.Core.dll)
Return Value
Type: System.Linq.Expressions.ConstantExpressionA 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. |
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:
// 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.
Console.WriteLine(constantExpr.ToString());
// This code example produces the following output:
//
// null
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.