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