Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The issue is that the == operator in C# is a call to a static method (well, maybe not technically, but it can be though of as such) based on the <em>compile time type</em> of the two parameters. What the actual runtime types of those objects are doesn't matter.</p> <p>Based on that compile time type the compiler will determine what implementation of <code>operator ==</code> to use. It might use the default <code>object</code> implementation, it might use one of the numeric overloads provided by the language, or it could be a user defined implementation.</p> <p>This is different from VB in that VB doesn't determine the implementation at compile time. It waits until runtime and inspects the two parameters that it is given to determine which implementation of the <code>==</code> operator it should use. </p> <p>Your code contains boolean values, but they are in variables that are of type <code>object</code>. Because the <em>variable</em> is of type <code>object</code>, the C# compiler use the <code>object</code> implementation of <code>==</code>, which compares the <em>references</em>, not the object instances. Since the boolean values are boxes, they don't have the same reference, even though their values are the same.</p> <p>The VB code doesn't care what type the variable is. It waits until runtime and then checks the two variables, sees that they are <em>actually</em> of both of type boolean, and so uses the boolean <code>==</code> operator implementation. That implementation compares the values of the booleans, not their references (and the booleans will be unboxed before calling calling that operator, so a reference comparison doesn't even make sense any more). Because the values of the booleans are the same, it returns true.</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