Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Update: </p> <p>The answer below seems to be specific to the case the <code>System.Object</code> is extended. When extending other classes there is no <code>NullReferenceException</code> in VB. </p> <p>This behavior is by design for the reason stated in this <a href="http://connect.microsoft.com/VisualStudio/feedback/details/295831/extension-methods-do-not-work-on-variables-of-type-object-in-visual-basic" rel="nofollow noreferrer">Connect issue</a>:</p> <blockquote> <p>VB allows you to call extension methods <em>defined</em> on Object, but only if the variable is not statically typed as Object.</p> <p>The reason is VB also supports late-binding, and if we bind to an extension method when you make a call off a variable declared as Object, then it's ambigous whether or not you're trying to call an extension method or a different late-bound method with the same name.</p> <p>Theoretically we could allow this with Strict On, but one of the principles of Option Strict is that it should not change the semantics of your code. If this was allowed then changing your Option Strict setting could cause a silent rebinding to a different method, resulting in totally different runtime behavior.</p> </blockquote> <p>Example:</p> <pre><code>Imports System.Runtime.CompilerServices Module Extensions &lt;Extension()&gt; _ Public Function IsNull(ByVal obj As Object) As Boolean Return obj Is Nothing End Function &lt;Extension()&gt; _ Public Function IsNull(ByVal obj As A) As Boolean Return obj Is Nothing End Function &lt;Extension()&gt; _ Public Function IsNull(ByVal obj As String) As Boolean Return obj Is Nothing End Function End Module Class A End Class Module Module1 Sub Main() ' works Dim someString As String = Nothing Dim isStringNull As Boolean = someString.IsNull() ' works Dim someA As A = Nothing Dim isANull As Boolean = someA.IsNull() Dim someObject As Object = Nothing ' throws NullReferenceException 'Dim someObjectIsNull As Boolean = someObject.IsNull() Dim anotherObject As Object = New Object ' throws MissingMemberException Dim anotherObjectIsNull As Boolean = anotherObject.IsNull() End Sub End Module </code></pre> <p>In fact, the VB compiler creates a late binding call in case your variable is statically typed as <code>Object</code>:</p> <pre><code>.locals init ([0] object exampleObject, [1] bool exists) IL_0000: ldnull IL_0001: stloc.0 IL_0002: ldloc.0 IL_0003: ldnull IL_0004: ldstr "IsNull" IL_0009: ldc.i4.0 IL_000a: newarr [mscorlib]System.Object IL_000f: ldnull IL_0010: ldnull IL_0011: ldnull IL_0012: call object [Microsoft.VisualBasic]Microsoft.VisualBasic. CompilerServices.NewLateBinding::LateGet( object, class [mscorlib]System.Type, string, object[], string[], class [mscorlib]System.Type[], bool[]) IL_0017: call object [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.Operators::NotObject(object) IL_001c: call bool [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.Conversions::ToBoolean(object) IL_0021: stloc.1 </code></pre>
    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