Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The cast to <code>string</code> in your code sample doesn't give the <code>null</code> a type, as <code>null</code> cannot have a type itself. If you want proof of this, then execute the following code where you can see that <code>null</code> is always equal to itself, irrespective of what the type of variable it was assigned to is:</p> <pre><code>string s = null; IPAddress i = null; Console.WriteLine(object.Equals(s, i)); // prints "True" Console.WriteLine(object.ReferenceEquals(s, i)); // prints "True" </code></pre> <p>What the cast does is tell the compiler which overload to choose. As <code>null</code> doesn't have a type it does not know whether to choose the overload that takes an <code>object</code> or a <code>string</code> as the value could be interpreted as either. So you're helping it out by saying "Here's a null value that should be treated as if it was a string".</p> <hr> <p>If you want to see what's going on underneath, then look at the IL from your code. The relevant bit for the method call is something like the following in textual IL (depending on your namespace and class name, etc):</p> <pre><code>ldnull call void ConsoleApplication1.Program::Foo(string) </code></pre> <p>So all that's happening is that a null is being loaded on the stack, and then this is consumed by the overload that takes the string, as overload resolution is performed at compile time so the method to call is baked into the IL.</p> <p>If you want to see what <code>ldnull</code> does, and why it's different from just using something like <code>ldc.i4.0</code> to load a zero onto the stack then see <a href="https://stackoverflow.com/questions/398966/how-is-null-represented-in-net/398978#398978">this answer</a> (if you don't want to follow the link, the reason is that it's a size-agnostic zero which otherwise doesn't exist in the CLR).</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. 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