Condividi tramite


Expression.AndAlso Method (Expression, Expression)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Creates a BinaryExpression that represents a conditional AND operation that evaluates the second operand only if the first operand evaluates to true.

Namespace:  System.Linq.Expressions
Assembly:  System.Core (in System.Core.dll)

Syntax

'Declaration
Public Shared Function AndAlso ( _
    left As Expression, _
    right As Expression _
) As BinaryExpression
public static BinaryExpression AndAlso(
    Expression left,
    Expression right
)

Parameters

Return Value

Type: System.Linq.Expressions.BinaryExpression
A BinaryExpression that has the NodeType property equal to AndAlso and the Left and Right properties set to the specified values.

Exceptions

Exception Condition
ArgumentNullException

left or right is nulla null reference (Nothing in Visual Basic).

InvalidOperationException

The bitwise AND operator is not defined for left.Type and right.Type.

-or-

left.Type and right.Type are not the same Boolean type.

Remarks

The resulting BinaryExpression has the Method property set to the implementing method. The Type property is set to the type of the node. If the node is lifted, the IsLifted and IsLiftedToNull properties are both true. Otherwise, they are false. The Conversion property is nulla null reference (Nothing in Visual Basic).

The following information describes the implementing method, the node type, and whether a node is lifted.

Implementing Method

The following rules determine the implementing method for the operation:

  • If the Type property of either left or right represents a user-defined type that overloads the bitwise AND operator, the MethodInfo that represents that method is the implementing method.

    NoteNote:

    The conditional AND operator cannot be overloaded in C# or Visual Basic. However, the conditional AND operator is evaluated by using the bitwise AND operator. Thus, a user-defined overload of the bitwise AND operator can be the implementing method for this node type.

  • Otherwise, if left.Type and right.Type are Boolean types, the implementing method is nulla null reference (Nothing in Visual Basic).

Node Type and Lifted versus Non-Lifted

If the implementing method is not nulla null reference (Nothing in Visual Basic):

  • If left.Type and right.Type are assignable to the corresponding argument types of the implementing method, the node is not lifted. The type of the node is the return type of the implementing method.

  • If the following two conditions are satisfied, the node is lifted and the type of the node is the nullable type that corresponds to the return type of the implementing method:

    • left.Type and right.Type are both value types of which at least one is nullable, and the corresponding non-nullable types are equal to the corresponding argument types of the implementing method.

    • The return type of the implementing method is a non-nullable value type.

If the implementing method is nulla null reference (Nothing in Visual Basic):

  • left.Type and right.Type are the same Boolean type.

  • If left.Type and right.Type are non-nullable, the node is not lifted. The type of the node is the result type of the predefined conditional AND operator.

  • If left.Type and right.Type are nullable, the node is lifted. The type of the node is the nullable type that corresponds to the result type of the predefined conditional AND operator.

Examples

The following code example shows how to create an expression that performs a logical AND operation on its two operands only if the first operand evaluates to true.

        ' Add the following directive to your file:
        ' Imports System.Linq.Expressions  

        ' This expression performs a logical AND operation
        ' on its two arguments, but if the first argument is false,
        ' the second argument is not evaluated.
        ' Both arguments must be of the Boolean type.
        Dim andAlsoExpr As Expression = Expression.AndAlso(
             Expression.Constant(False),
             Expression.Constant(True)
         )

        ' Print the expression.
        outputBlock.Text &= andAlsoExpr.ToString() & vbCrLf

        ' The following statement first creates an expression tree,
        ' then compiles it, and then executes it. 
        outputBlock.Text &= Expression.Lambda(Of Func(Of Boolean))(andAlsoExpr).Compile()() & vbCrLf

        ' This code example produces the following output:
        '
        ' (False AndAlso True)
        ' False

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

       // This expression perfroms a logical AND operation
       // on its two arguments, but if the first argument is false,
       // then the second arument is not evaluated.
       // Both arguments must be of the boolean type.
       Expression andAlsoExpr = Expression.AndAlso(
           Expression.Constant(false),
           Expression.Constant(true)
       );

       // Print out the expression.
       outputBlock.Text += andAlsoExpr.ToString() + "\n";

       // The following statement first creates an expression tree,
       // then compiles it, and then executes it. 
       outputBlock.Text += Expression.Lambda<Func<bool>>(andAlsoExpr).Compile()() + "\n";

       // This code example produces the following output:
       //
       // (False AndAlso True)
       // False

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.