.NET API Documentation

Class System.Linq.Expressions.BinaryExpression

Represents an operation that takes two operands, such as addition, subtraction, and equality checks.

The BinaryExpression class is used to represent binary operations in the expression tree API. It contains properties for the left and right operands, the type of the operation (represented by ExpressionType), and optionally a shortcut `delegateType` and a `method` information for certain operations like overloaded operators. Instances of BinaryExpression are created using the factory methods provided by the Expression class.

Inheritance

object > System.Linq.Expressions.Expression > System.Linq.Expressions.BinaryExpression

Namespace

Assembly

System.Linq.Expressions.dll

Members

Properties

  • Left

    Gets the left operand of the binary operation.

    public System.Linq.Expressions.Expression Left { get; }
  • Right

    Gets the right operand of the binary operation.

    public System.Linq.Expressions.Expression Right { get; }
  • NodeType

    Gets the ExpressionType that represents the expression as the root of the expression tree.

    public override System.Linq.Expressions.ExpressionType NodeType { get; }
  • Type

    Gets the static type of the expression that this Expression represents.

    public override System.Type Type { get; }
  • IsLifted

    Gets a value that indicates whether the expression is lifted to an overload-resolution common type.

    public bool IsLifted { get; }
  • IsLiftedToNull

    Gets a value that indicates whether the expression is lifted to a nullable type.

    public bool IsLiftedToNull { get; }
  • Method

    Gets the MethodInfo representing the implementing method for the binary operation.

    public System.Reflection.MethodInfo Method { get; }
  • Conversion

    Gets the LambdaExpression that represents the conversion for the lifted binary operation.

    public System.Linq.Expressions.LambdaExpression Conversion { get; }

Methods

  • Update(System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right)

    Returns a new BinaryExpression that replaces the child expressions, the Type, and the Method of the current BinaryExpression with the specified values.

    public System.Linq.Expressions.BinaryExpression Update(System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right)
    This method allows you to create a new expression tree node that is a copy of the current node, but with specified children replaced. This is useful for rewriting expression trees.
  • Update(System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right, System.Type type, System.Reflection.MethodInfo method)

    Returns a new BinaryExpression that replaces the child expressions, the Type, and the Method of the current BinaryExpression with the specified values.

    public System.Linq.Expressions.BinaryExpression Update(System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right, System.Type type, System.Reflection.MethodInfo method)
    This overload allows for more granular control over the created BinaryExpression, including specifying the target type and the method used for the operation.
  • Update(System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right, System.Type type, System.Reflection.MethodInfo method, System.Linq.Expressions.LambdaExpression conversion)

    Returns a new BinaryExpression that replaces the child expressions, the Type, and the Method of the current BinaryExpression with the specified values.

    public System.Linq.Expressions.BinaryExpression Update(System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right, System.Type type, System.Reflection.MethodInfo method, System.Linq.Expressions.LambdaExpression conversion)
    This overload is used for lifted binary operations, allowing the specification of a conversion lambda expression.

Example

// Create an expression tree that represents 1 + 2
var left = Expression.Constant(1, typeof(int));
var right = Expression.Constant(2, typeof(int));
var body = Expression.Add(left, right); // Creates a BinaryExpression
var addExpression = Expression.Lambda<Func<int>>(body);

// Compile and execute the expression
var result = addExpression.Compile()(); // result will be 3
Console.WriteLine(result);

See Also