Прочитать на английском

Поделиться через


Expression.Default(Type) Метод

Определение

Создает DefaultExpression, для свойства Type которого задан указанный тип.

public static System.Linq.Expressions.DefaultExpression Default (Type type);

Параметры

type
Type

Объект Type, который следует задать в качестве значения свойства Type.

Возвращаемое значение

Выражение 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

Применяется к

Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0