Expression.Default(Type) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
创建一个 DefaultExpression,Type 属性设置为指定类型。
public:
static System::Linq::Expressions::DefaultExpression ^ Default(Type ^ type);
public static System.Linq.Expressions.DefaultExpression Default (Type type);
static member Default : Type -> System.Linq.Expressions.DefaultExpression
Public Shared Function Default (type As Type) As DefaultExpression
参数
返回
一个 DefaultExpression,其 NodeType 属性等于 Default,并且其 Type 属性设置为指定类型。
示例
下面的代码示例演示如何创建表示给定类型的默认值的表达式。
// Add the following directive to your file:
// using System.Linq.Expressions;
// This expression represents the default value of a type
// (0 for integer, null for a string, etc.)
Expression defaultExpr = Expression.Default(
typeof(byte)
);
// Print out the expression.
Console.WriteLine(defaultExpr.ToString());
// The following statement first creates an expression tree,
// then compiles it, and then executes it.
Console.WriteLine(
Expression.Lambda<Func<byte>>(defaultExpr).Compile()());
// This code example produces the following output:
//
// default(Byte)
// 0
' Add the following directive to your file:
' Imports System.Linq.Expressions
' This expression represents the default value of a type
' (0 for integer, null for a string, and so on).
Dim defaultExpr As Expression = Expression.Default(
GetType(Byte)
)
' Print the expression.
Console.WriteLine(defaultExpr.ToString())
' The following statement first creates an expression tree,
' then compiles it, and then executes it.
Console.WriteLine(
Expression.Lambda(Of Func(Of Byte))(defaultExpr).Compile()())
' This code example produces the following output:
'
' default(Byte)
' 0