- Why is operator ‘&’ defined for bool?, and operator ‘&&’ is not?
- 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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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:
Likewise, from the ECMA spec, 14.2.7 Lifted operators, the lifted operators are:
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: theint‘+’ operator gets ‘lifted’ ontoint?, 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
decimalandDateTimeprovide various operator overloadsShort-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:
if
Method1()returns false, thenMethod2()isn’t executed (since the compiler already knows that the overall answer is false). This is important ifMethod2()has side-effects, since as saving to the database…