In VB.NET, what is the difference between
if foo is Nothing Then
doStuff()
End If
and
if foo=Nothing Then
doStuff
Here's some IL to validate the differences:
.method public static void Main() cil managed
{
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor()
.entrypoint
.maxstack 3
.locals init (
[0] object o,
[1] bool VB$CG$t_bool$S0)
L_0000: nop
L_0001: newobj instance void [mscorlib]System.Object::.ctor()
L_0006: call object [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::GetObjectValue(object)
L_000b: stloc.0
L_000c: ldloc.0
L_000d: ldnull
L_000e: ceq
L_0010: stloc.1
L_0011: ldloc.1
L_0012: brfalse.s L_001f
L_0014: ldstr "Is Nothing"
L_0019: call void [mscorlib]System.Console::WriteLine(string)
L_001e: nop
L_001f: nop
L_0020: ldloc.0
L_0021: ldnull
L_0022: ldc.i4.0
L_0023: call bool [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.Operators::ConditionalCompareObjectEqual(object, object, bool)
L_0028: stloc.1
L_0029: ldloc.1
L_002a: brfalse.s L_0037
L_002c: ldstr "Is nothing"
L_0031: call void [mscorlib]System.Console::WriteLine(string)
L_0036: nop
L_0037: nop
L_0038: nop
L_0039: ret
}
VB Code:
Sub Main()
Dim o As New Object
If o Is Nothing Then
Console.WriteLine("Is Nothing")
End If
If o = Nothing Then
Console.WriteLine("Is nothing")
End If
End Sub