Represents an expression that has two operands, such as Add, Subtract, Multiply, Divide, Equal, NotEqual, LessThan, LessThanOrEqual, GreaterThan, GreaterThanOrEqual, And, Or, Xor, LeftShift, RightShift, AddAssign, SubtractAssign, MultiplyAssign, DivideAssign, AndAssign, OrAssign, XorAssign, LeftShiftAssign, RightShiftAssign, Power, and PowerAssign.
public class BinaryExpression : Expression
System.ObjectSystem.Linq.Expressions.ExpressionSystem.Linq.Expressions.BinaryExpression
| Name | Description |
|---|---|
Left |
Gets the left operand of the binary operation. |
Method |
Gets the implementing method for the binary operation. This is relevant for user-defined operators. |
NodeType |
Gets the ExpressionType that represents the expression. For BinaryExpression, this will be one of the binary operators (e.g., ExpressionType.Add). |
Right |
Gets the right operand of the binary operation. |
Type |
Gets the static type of the expression that results from the binary operation. |
IsLifted |
Gets a value that indicates whether the binary operation is lifted to handle null values. |
IsLiftedToNull |
Gets a value that indicates whether the binary operation is lifted to its resulting type and the result is a null value. |
| Name | Description |
|---|---|
Update |
Replaces the children of the BinaryExpression with the specified new children. |
The following are common operators that result in a BinaryExpression:
+, -, *, /, %==, !=, <, >, <=, >=&&, ||, ^&, |, ^, <<, >>+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=The following code example demonstrates how to create a BinaryExpression that represents the addition of two integer variables:
using System;
using System.Linq.Expressions;
public class Example
{
public static void Main(string[] args)
{
// Represents the expression 'x + y'
ParameterExpression x = Expression.Parameter(typeof(int), "x");
ParameterExpression y = Expression.Parameter(typeof(int), "y");
BinaryExpression addExpression = Expression.Add(x, y);
// Compile and run the expression
var lambda = Expression.Lambda<Func<int, int, int>>(addExpression, x, y);
var compiledDelegate = lambda.Compile();
int result = compiledDelegate(5, 3);
Console.WriteLine($"5 + 3 = {result}"); // Output: 5 + 3 = 8
// Example of a comparison
BinaryExpression greaterThanExpression = Expression.GreaterThan(x, y);
var compiledComparison = Expression.Lambda<Func<int, int, bool>>(greaterThanExpression, x, y).Compile();
bool isGreater = compiledComparison(7, 3);
Console.WriteLine($"Is 7 greater than 3? {isGreater}"); // Output: Is 7 greater than 3? True
}
}