Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There's no implicit conversion from <code>Nullable&lt;bool&gt;</code> to <code>bool</code>. There <em>is</em> an implicit conversion from <code>bool</code> to <code>Nullable&lt;bool&gt;</code> and that's what happens (in language terms) to each of the bool constants in the first version. The <code>bool operator==(Nullable&lt;bool&gt;, Nullable&lt;bool&gt;</code> operator is then applied. (This isn't quite the same as other lifted operators - the result is just <code>bool</code>, not <code>Nullable&lt;bool&gt;</code>.)</p> <p>In other words, the expression 'fred == false' is of type <code>bool</code>, whereas the expression 'fred' is of type <code>Nullable&lt;bool&gt;</code> hence you can't use it as the "if" expression.</p> <p>EDIT: To answer the comments, there's never an implicit conversion from <code>Nullable&lt;T&gt;</code> to <code>T</code> and for good reason - implicit conversions shouldn't throw exceptions, and unless you want <code>null</code> to be implicitly converted to <code>default(T)</code> there's not a lot else that could be done.</p> <p>Also, if there <em>were</em> implicit conversions both ways round, an expression like "nullable + nonNullable" would be very confusing (for types that support +, like <code>int</code>). Both +(T?, T?) and +(T, T) would be available, depending on which operand were converted - but the results could be very different!</p> <p>I'm 100% behind the decision to only have an explicit conversion from <code>Nullable&lt;T&gt;</code> to <code>T</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload