How to override && (logical and operator)?

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 the "Expected Overloaded Binary Operator". What am I doing wrong?

+6
source share
4 answers

Conditional logic operators cannot be overloaded.

According to the documentation :

Conditional logical operators cannot be overloaded, but they are evaluated using & and | that may be overloaded.

This article provides additional information on how to implement your own and and || operators.

+13
source

You cannot 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); } 
+8
source

See page

+5
source

From this :

& &, ||: Conditional logical operators cannot be overloaded, but they are evaluated using and and | which can be overloaded.

Therefore, you cannot override this, but you can override & or | .

+3
source

All Articles