Expression.IfThenElse(Expression, Expression, Expression) Yöntem

Tanım

ve else deyimleriyle if koşullu bloğu temsil eden bir ConditionalExpression oluşturur.

C#
public static System.Linq.Expressions.ConditionalExpression IfThenElse (System.Linq.Expressions.Expression test, System.Linq.Expressions.Expression ifTrue, System.Linq.Expressions.Expression ifFalse);

Parametreler

test
Expression

Expression özelliğinin Test değerine eşit olarak ayarlanması için.

ifTrue
Expression

Expression özelliğinin IfTrue değerine eşit olarak ayarlanması için.

ifFalse
Expression

Expression özelliğinin IfFalse değerine eşit olarak ayarlanması için.

Döndürülenler

ConditionalExpression özelliğine ConditionalNodeType eşit olan ve Test, IfTrueve IfFalse özellikleri belirtilen değerlere ayarlanmış. Bu yöntem tarafından döndürülen sonucun ConditionalExpression türü şeklindedir Void.

Örnekler

Aşağıdaki kod örneği, koşullu bloğu temsil eden bir ifadenin nasıl oluşturulacağını gösterir.

C#
// Add the following directive to the file:
// using System.Linq.Expressions;
bool test = true;

// This expression represents the conditional block.
Expression ifThenElseExpr = Expression.IfThenElse(
    Expression.Constant(test),
    Expression.Call(
        null,
        typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
        Expression.Constant("The condition is true.")
    ),
    Expression.Call(
        null,
        typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
        Expression.Constant("The condition is false.")
    )
);

// The following statement first creates an expression tree,
// then compiles it, and then runs it.
Expression.Lambda<Action>(ifThenElseExpr).Compile()();

// This code example produces the following output:
//
// The condition is true.

Şunlara uygulanır

Ürün Sürümler
.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