There are two weird operators in C#:
If I understand this right these operators can be used in types which I want to use instead of a boolean expression and where I don't want to provide an implicit conversion to bool.
Let's say I have a following class:
public class MyType { public readonly int Value; public MyType(int value) { Value = value; } public static bool operator true (MyType mt) { return mt.Value > 0; } public static bool operator false (MyType mt) { return mt.Value < 0; } }
So I can write the following code:
MyType mTrue = new MyType(100); MyType mFalse = new MyType(-100); MyType mDontKnow = new MyType(0); if (mTrue) { // Do something. } while (mFalse) { // Do something else. } do { // Another code comes here. } while (mDontKnow)
However for all the examples above only the true operator is executed. So what's the false operator in C# good for?
Note: More examples can be found here, here and here.
C does not have boolean data types, and normally uses integers for boolean testing. Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true.
&& is the logical AND operator used for logical tests. For example: "if(a && b) { i++;}". Any value different from 0 is considered TRUE. In this case: a && b is the same as 0 && 5 that is the same as FALSE AND TRUE.
AND “&&” finds the first falsy value The AND && operator does the following: Evaluates operands from left to right. For each operand, converts it to a boolean. If the result is false , stops and returns the original value of that operand.
You can use it to override the &&
and ||
operators.
The &&
and ||
operators can't be overridden, but if you override |
, &
, true
and false
in exactly the right way the compiler will call |
and &
when you write ||
and &&
.
For example, look at this code (from http://ayende.com/blog/1574/nhibernate-criteria-api-operator-overloading - where I found out about this trick; archived version by @BiggsTRC):
public static AbstractCriterion operator &(AbstractCriterion lhs, AbstractCriterion rhs) { return new AndExpression(lhs, rhs); } public static AbstractCriterion operator |(AbstractCriterion lhs, AbstractCriterion rhs) { return new OrExpression(lhs, rhs); } public static bool operator false(AbstractCriterion criteria) { return false; } public static bool operator true(AbstractCriterion criteria) { return false; }
This is obviously a side effect and not the way it's intended to be used, but it is useful.
Shog9 and Nir: thanks for your answers. Those answers pointed me to Steve Eichert article and it pointed me to msdn:
The operation x && y is evaluated as T.false(x) ? x : T.&(x, y), where T.false(x) is an invocation of the operator false declared in T, and T.&(x, y) is an invocation of the selected operator &. In other words, x is first evaluated and operator false is invoked on the result to determine if x is definitely false. Then, if x is definitely false, the result of the operation is the value previously computed for x. Otherwise, y is evaluated, and the selected operator & is invoked on the value previously computed for x and the value computed for y to produce the result of the operation.
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