Everything else seems to follow this pattern, but when I try:
public static ColumnOperation operator&&(ColumnOperation lhs, ColumnOperation rhs)
{
return new ColumnBooleanOperation(lhs, rhs, ExpressionType.And);
}
I get "Overloadable binary operator expected". What am I doing wrong?
Conditional logic operators cannot be overloaded.
According to the documentation:
The conditional logical operators cannot be overloaded, but they are evaluated using & and |, which can be overloaded.
This article provides more information on how to implement your own custom && and || operators.
You can't overload &&
directly, but you can overload the false
, true
and &
operators - see operator &&
public static bool operator true(ColumnOperation x)
{
...
}
public static bool operator false(ColumnOperation x)
{
...
}
public static ColumnOperation operator &(ColumnOperation lhs, ColumnOperation rhs)
{
return new ColumnBooleanOperation(lhs, rhs, ExpressionType.And);
}
This gives the same functionality. Specifically, from here:
The operation
x && y
is evaluated asT.false(x) ? x : T.&(x, y)
[...]x
is first evaluated and operatorfalse
is invoked on the result to determine ifx
is definitely false. Then, ifx
is definitely false, the result of the operation is the value previously computed forx
.
This indicates that short-circuit evaluation will also work as expected when the above overloads are implemented correctly. Overloading the |
can also be done in a similar fashion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With