Expression.Condition Method

Definition

Creates a ConditionalExpression that represents a conditional statement.

Overloads

Condition(Expression, Expression, Expression)

Creates a ConditionalExpression that represents a conditional statement.

Condition(Expression, Expression, Expression, Type)

Creates a ConditionalExpression that represents a conditional statement.

Condition(Expression, Expression, Expression)

Source:
ConditionalExpression.cs
Source:
ConditionalExpression.cs
Source:
ConditionalExpression.cs

Creates a ConditionalExpression that represents a conditional statement.

public static System.Linq.Expressions.ConditionalExpression Condition (System.Linq.Expressions.Expression test, System.Linq.Expressions.Expression ifTrue, System.Linq.Expressions.Expression ifFalse);

Parameters

test
Expression

An Expression to set the Test property equal to.

ifTrue
Expression

An Expression to set the IfTrue property equal to.

ifFalse
Expression

An Expression to set the IfFalse property equal to.

Returns

A ConditionalExpression that has the NodeType property equal to Conditional and the Test, IfTrue, and IfFalse properties set to the specified values.

Exceptions

test or ifTrue or ifFalse is null.

test.Type is not Boolean.

-or-

ifTrue.Type is not equal to ifFalse.Type.

Examples

The following code example shows how to create an expression that represents a conditional statement. If the first argument evaluates to true, the second argument is executed; otherwise, the third argument is executed.

// Add the following directive to your file:
// using System.Linq.Expressions;

int num = 100;

// This expression represents a conditional operation.
// It evaluates the test (first expression) and
// executes the iftrue block (second argument) if the test evaluates to true,
// or the iffalse block (third argument) if the test evaluates to false.
Expression conditionExpr = Expression.Condition(
                           Expression.Constant(num > 10),
                           Expression.Constant("num is greater than 10"),
                           Expression.Constant("num is smaller than 10")
                         );

// Print out the expression.
Console.WriteLine(conditionExpr.ToString());

// The following statement first creates an expression tree,
// then compiles it, and then executes it.
Console.WriteLine(
    Expression.Lambda<Func<string>>(conditionExpr).Compile()());

// This code example produces the following output:
//
// IIF("True", "num is greater than 10", "num is smaller than 10")
// num is greater than 10

Remarks

The Type property of the resulting ConditionalExpression is equal to the Type property of ifTrue.

See also

Applies to

.NET 9 and other versions
Product Versions
.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 3.5, 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

Condition(Expression, Expression, Expression, Type)

Source:
ConditionalExpression.cs
Source:
ConditionalExpression.cs
Source:
ConditionalExpression.cs

Creates a ConditionalExpression that represents a conditional statement.

public static System.Linq.Expressions.ConditionalExpression Condition (System.Linq.Expressions.Expression test, System.Linq.Expressions.Expression ifTrue, System.Linq.Expressions.Expression ifFalse, Type type);

Parameters

test
Expression

An Expression to set the Test property equal to.

ifTrue
Expression

An Expression to set the IfTrue property equal to.

ifFalse
Expression

An Expression to set the IfFalse property equal to.

type
Type

A Type to set the Type property equal to.

Returns

A ConditionalExpression that has the NodeType property equal to Conditional and the Test, IfTrue, and IfFalse properties set to the specified values.

Remarks

This method allows explicitly unifying the result type of the conditional expression in cases where the types of ifTrue and ifFalse expressions are not equal. Types of both ifTrue and ifFalse must be implicitly reference assignable to the result type. The type is allowed to be Void.

Applies to

.NET 9 and other versions
Product Versions
.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