What is the difference between 'foo = Nothing' and 'foo is Nothing' in VB.NET?

后端 未结 6 767
醉梦人生
醉梦人生 2021-01-02 07:12

In VB.NET, what is the difference between

if foo is Nothing Then
      doStuff()
End If

and

if foo=Nothing Then
    doStuff         


        
6条回答
  •  有刺的猬
    2021-01-02 07:25

    Asume:

    MyFunc(Foo as object)

    Foo - Boxed if ValueType

    if foo is Nothing Then

    object.ReferenceEquals (Code Inlined - Fastest method)

    if foo=Nothing Then

    Operators.ConditionalCompareObjectEqual(foo, Nothing, False)

    Vb.Net carry this case like Obj1 = Obj2. It do´nt uses Obj.equals(obj2) ! Exception if Obj1 is nothing

    This option uses a very complex code as there are a lot options depending all posible foo definitions.

    try this:

    Sub Main()
      Dim o As Object = 0
      Debug.Print(o Is Nothing)  'False
      Debug.Print(o = Nothing)   'True 
    End Sub
    

提交回复
热议问题