Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It depends on the type.</p> <ul> <li><p>For <strong>value types</strong>, <code>Is</code> <em>doesn’t work</em>, only <code>=</code>, and <code>Nothing</code> refers to the default instance of that type (i.e. the instance that you get by calling <code>New T()</code> for a given type <code>T</code>).</p></li> <li><p>For <strong>reference types</strong>, <code>Is</code> performs a reference comparison (identical to <code>object.ReferenceEquals(a, Nothing)</code>). <code>a = Nothing</code> usually <em>does not work</em>, <strong>unless</strong> <code>Operator =</code> has explicitly been defined for that class.</p> <p>If, furthermore, <code>Operator =</code> has been implemented correctly, then <code>foo = Nothing</code> and <code>foo Is Nothing</code> should yield the same result (but the same isn’t true for any other value instead of <code>Nothing</code>) but <code>foo Is Nothing</code> will be more efficient since it’s a compiler intrinsic while <code>Operator =</code> will call a method.</p></li> <li><p>For <strong>nullable value types</strong> (i.e. instances of <code>Nullable(Of T)</code>), special rules apply: like all other operators, <code>=</code> is <a href="http://blogs.msdn.com/b/ericlippert/archive/2007/06/27/what-exactly-does-lifted-mean.aspx" rel="nofollow noreferrer"><em>lifted</em></a> (notice the error in that blog post …) by the compiler to the underlying type. The result of comparing two <code>Nullable</code>s is thus not <code>Boolean</code> but <code>Boolean?</code> (note the <code>?</code>). However, because of so-called “null propagation” for lifted operators, this will <em>always</em> return <code>Nothing</code>, no matter the value of <code>foo</code>. Quoting the <a href="http://download.microsoft.com/download/D/B/1/DB1E9BCC-A878-4BCC-880F-71387EAAF1E1/Visual%20Basic%20Language%20Specification%2010.0.doc" rel="nofollow noreferrer">Visual Basic 10 language specification</a> (§1.86.3):</p> <blockquote> <p>If ether (sic!) operand is <code>Nothing</code>, the result of the expression is a value of <code>Nothing</code> typed as the nullable version of the result type.</p> </blockquote> <p>So if the users want to compare a <code>Nullable</code> variable to <code>Nothing</code>, they must use the <code>foo Is Nothing</code> syntax for which, once again, the compiler generates special code to make it work (§1.79.3 of the Visual Basic 10 language specification). <sub>Hat tip to Jonathan Allen for (correctly) persisting that I was wrong; hat tip to Jared Parsons for passing me a link to the Visual Basic 10 specification.</sub></p></li> </ul> <p>(The above assumes that <code>Option Strict On</code> is used, as you <em>always</em> should. In case that isn’t the case, the results will differ slightly since calling <code>foo = Nothing</code> may perform a late-bound call.)</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