Expression.Default(Type) Methode

Definition

Erstellt eine DefaultExpression Eigenschaft, die auf Type den angegebenen Typ festgelegt ist.

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

Parameter

type
Type

A Type , um die Type Eigenschaft gleich festzulegen.

Gibt zurück

A DefaultExpression that has the NodeType property equal to Default and the Type property set to the specified type.

Beispiele

Das folgende Codebeispiel zeigt, wie Sie einen Ausdruck erstellen, der einen Standardwert für einen bestimmten Typ darstellt.

// 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

Gilt für: