Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Tejas's answer is correct. To more specifically address some of your points:</p> <blockquote> <p>Why does the following code not result in compile error? </p> </blockquote> <p>The question is not answerable; it does not produce an error because it is legal code, but that's a tautology. </p> <p>If your question is actually "which section of the C# specification makes this legal?", then that's an answerable question. The section on lifted equality operators makes it legal.</p> <blockquote> <p>It's comparing a reference type to primitive type. </p> </blockquote> <p>It is not. First off, avoid the term "primitive type"; the specification does not clearly define it and it is not a useful concept in C#. You meant to say I think that it is comparing a value of reference type to a value of value type.</p> <p>Second, that's not correct either. The null literal is not of reference type or value type; it is of no type. It is <em>convertible</em> to any nullable value type or any reference type, but it is of no type just by itself.</p> <p>In this case the null literal is converted to the nullable bool type.</p> <blockquote> <p>Both null and false have to be interpreted into something for the compiler to do comparison. </p> </blockquote> <p>Correct. They are interpreted as nullable bools.</p> <blockquote> <p>is the parser simply scanning for such pattern and replace it with false?</p> </blockquote> <p>No, but that's an excellent guess. The compiler <em>would</em> constant-fold, say, <code>true == false</code> down to <code>false</code>, but it does not do folding optimizations that involve nullable value types. The language could be redesigned to support constant folding on operations with nullable value type operands; had nullable value types counterfactually been in version one, the proposed feature likely would have been supported.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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