8
  1. Why is operator '&' defined for bool?, and operator '&&' is not?
  2. How exactly does this 1) bool? & bool? and 2) bool? and bool work?

Any other "interesting" operator semantics on Nullable? Any overloaded operators for generic T?

Paul Kapustin
  • 3,297
  • 5
  • 35
  • 45

4 Answers4

14

Operators on Nullable<T> are "lifted" operators. What this means is: if T has the operator, T? will have the "lifted" counterpart.

&& and || aren't really operators in the same sense as & and | - for example, they can't be overloaded - from the ECMA spec 14.2.2 Operator overloading:

The overloadable binary operators are: + - * / % & | ^ << >> == != > < >= <= Only the operators listed above can be overloaded. In particular, it is not possible to overload member access, method invocation, or the =, &&, ||, ??, ?:, checked, unchecked, new, typeof, as, and is operators.

Likewise, from the ECMA spec, 14.2.7 Lifted operators, the lifted operators are:

For the unary operators + ++ - -- ! ~

For the binary operators + - * / % & | ^ << >>

For the equality operators == !=

For the relational operators < > <= >=

So basically, the short-circuiting operators aren't defined as lifted operators.

[edit: added crib sheet]

  • Lifted operator: a compiler provided operator on Nullable<T>, based on the operators of T - for example: the int "+" operator gets "lifted" onto int?, defined as:

    (int? x, int? y) => (x.HasValue && y.HasValue) ? (x.Value + y.Value) : (int?) null;

  • Operator overloading: the act of providing a custom operator implementation for a given type; for example decimal and DateTime provide various operator overloads

  • Short-circuiting: the normal behavior of && and || (in many languages, including C++ and C#) - i.e. the second operand might not be evaluated - i.e.

    (expression1, expression2) => expression1() ? expression2() : false;

Or perhaps a simpler example:

bool someFlag = Method1() && Method2();

if Method1() returns false, then Method2() isn't executed (since the compiler already knows that the overall answer is false). This is important if Method2() has side-effects, since as saving to the database...

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Thanks. However, after C++, all this terminology...short-circuit, lifted...impossibility to override &&...sounds quite strange to me :) – Paul Kapustin Nov 19 '08 at 22:01
4

There are no short-circuiting operators (&& ||) defined for bool?

Only are the logical AND, inclusive OR, operators and they behave like this:

x        y      x & y   x | y   
true    true    true    true
true    false   false   true
true    null    null    true
false   true    false   true
false   false   false   false
false   null    false   null
null    true    null    true
null    false   false   null
null    null    null    null
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • Thanks. Maybe can also explain why they are called short-circuiting, these && and || ? – Paul Kapustin Nov 19 '08 at 20:40
  • 1
    They are "short-circuiting" because they only evaluate multiple conditions in a logical statement if necessary. See http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Logical_Operators for more information. – Scott Dorman Nov 19 '08 at 21:05
  • @ScottDorman To be clear for other readers, in C# the operators | or & are NOT short-circuiting for bool?, even in the latest C# 10. (Your comment did not tag the previous commenter, and it wasn't immediately clear you were responding to the comment rather than the answer.) – C Perkins Mar 13 '22 at 06:18
1

Operator "&" is bitwise operator, ¨whereas "&&" is logical operator. The logcial "AND" operator can not be aplied to the threestate value, only for twostate.

TcKs
  • 25,849
  • 11
  • 66
  • 104
0

-- In addition, the logical && operator along with the || operator cannot be overloaded.

2) The reason that the bitwise operator works is because it operates on single bits, which is a simple 1 or 0. This is what boolean operators essentially are, true and false, 1 and 0.

Craig Wilson
  • 12,174
  • 3
  • 41
  • 45