c# - Behaviour of nullable boolean -


why 2nd case not compile? there way make work overloading or sth?

bool b1 = true; bool? b2 = false;  if (b1) //does compile {     //do sth. }  if (b2) //doesn't compile {     //do sth. }  if (b2 == true) //does compile {     //do sth. } 

the reason why third example works because of lifted operators. so:

for equality operators

== !=

a lifted form of operator exists if operand types both non-nullable value types , if result type bool. lifted form constructed adding single ? modifier each operand type. lifted operator considers 2 null values equal, , null value unequal non-null value. if both operands non-null, lifted operator unwraps operands , applies underlying operator produce bool result.

so, that's why third example works - because of machinery has been invented , works all nullable types.

your second example could have been made work have required compiler perform special work one nullable type - bool?. it's not appropriate, in general, every use of nullable type perform implicit conversion original value type.


(quote above section 7.3.7 of language specification)


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -