I should add that the IL code I gave is for the Debug build, the Release
build's IL is optimized slightly. Plus the JIT will optimize the Release
build even more, such as inlining the GetObjectValue and/or the IsNothing
method themselves. So performance wise you really need to profile the
routine & determine if that routine needs to be "micro optimized" by
choosing IsNothing() over Is Nothing.
As I & others stated, its personal preference.
--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley -
http://www.tsbradley.net
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote in
message news:eN**************@TK2MSFTNGP09.phx.gbl...
| Steve,
| In addition to the other comments. Its largely personal preference.
|
| However:
|| 1: If IsNothing(myobject) ...
|
| Is two function calls into a library, which may or may not be in-lined by
| the JIT compiler.
| //000038: If IsNothing(myobject) Then
| IL_0003: ldloc.2
| IL_0004: call object
|
[mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::Ge tObjectValue(object)
| IL_0009: call bool
|
[Microsoft.VisualBasic]Microsoft.VisualBasic.Information::IsNothing(objec t)
| IL_000e: stloc.s VB$CG$t_bool$S0
| IL_0010: ldloc.s VB$CG$t_bool$S0
| IL_0012: brfalse.s IL_0014
| //000039:
| //000040: End If
| IL_0014: nop
|
|
| While:
|
|| 2. If myobject Is Nothing ...
|
| Is inline IL code.
| //000041: If myobject Is Nothing Then
| IL_0015: ldloc.2
| IL_0016: ldnull
| IL_0017: ceq
| IL_0019: stloc.s VB$CG$t_bool$S0
| IL_001b: ldloc.s VB$CG$t_bool$S0
| IL_001d: brfalse.s IL_001f
| //000042:
| //000043: End If
| IL_001f: nop
|
|
| FWIW: the "patented" IsNot operator also generates IL code. Oddly enough
it
| generates the same code that C#'s reference inequality operator has been
| generating since C# 1.0.
|
| --
| Hope this helps
| Jay [MVP - Outlook]
| .NET Application Architect, Enthusiast, & Evangelist
| T.S. Bradley -
http://www.tsbradley.net
|
|
| "Steve Peterson" <sp*******@nospam.com> wrote in message
| news:%2****************@TK2MSFTNGP14.phx.gbl...
|| Hi
||
|| I'm curious - which is the better ".NET" method to use if needing to know
| if
|| an object is Nothing?
||
|| 1: If IsNothing(myobject) ...
||
|| or
||
|| 2. If myobject Is Nothing ...
||
|| TIA
|| Steve
||
||
|
|