In VB.net, what is the difference between
if foo is Nothing Then
doStuff()
End If
and
if foo=Nothing Then
doStuff()
End If
Update I received the following answer:
foo is Nothing simply checks if
foo
is not assigned to any reference.
foo=Nothing checks if the reference held byfoo
is equal tonothing
After running the following three statements:
Dim foo as Object
Dim bar as Integer
foo = bar
foo is Nothing
evaluates to false and foo = Nothing
evaluates to true.
However, if bar
is declared as an Object
and not initialized, then foo is Nothing
and foo = Nothing
both evaluate to true! I think this is because Integer
is a value type and Object
is a reference type.