String empty test

  • Thread starter Thread starter Julie
  • Start date Start date
I added an external tool with

Command: C:\Program\Microsoft Visual Studio .NET
2003\SDK\v1.1\Bin\ildasm.exe
Arguments: $(TargetName)$(TargetExt)
Initial Directory: $(TargetDir)

I get the disassembler but I can not see any methods onyl variables in my
classes ?

//Andreas

Andreas Håkansson said:
Opps, make that Ildasm.exe =)

Andreas Håkansson said:
Willy,

Is there any nice way to add the ilasm.exe as a tool to vs.net so
I wont have to command line it each time? Maybe then I'd go the IL
way more often to check things out =)

//Andreas

Willy Denoyette said:
The (IMO) shortest IL codepath generated by the C# compiler (with optimize
option on).

Here it is:

.method public hidebysig static bool MyNullOrEmpty(string 'value') cil
managed
{
// Code size 15 (0xf)
.maxstack 8
IL_0000: ldarg.0
IL_0001: brfalse.s IL_000d
IL_0003: ldarg.0
IL_0004: callvirt instance int32 [mscorlib]System.String::get_Length()
IL_0009: ldc.i4.0
IL_000a: ceq
IL_000c: ret
IL_000d: ldc.i4.1
IL_000e: ret
} // end of method Tester::MyNullOrEmpty

Willy.

"Andreas Håkansson" <andy.h (at) telia.com> wrote in message
Willy,

I've never actually went down the IL road (I really should), do you
mean
shortest as in "you wont have to create an class instance" or
shorter
 
As always, things work when you check the right class ;) This I can't figure
out
though: Is there anyway to "follow" calls such as

IL_0001: callvirt instance int32 [mscorlib]System.String::get_Length()

to view thier IL directly ?

//Andreas

Andreas Håkansson said:
Opps, make that Ildasm.exe =)

Andreas Håkansson said:
Willy,

Is there any nice way to add the ilasm.exe as a tool to vs.net so
I wont have to command line it each time? Maybe then I'd go the IL
way more often to check things out =)

//Andreas

Willy Denoyette said:
The (IMO) shortest IL codepath generated by the C# compiler (with optimize
option on).

Here it is:

.method public hidebysig static bool MyNullOrEmpty(string 'value') cil
managed
{
// Code size 15 (0xf)
.maxstack 8
IL_0000: ldarg.0
IL_0001: brfalse.s IL_000d
IL_0003: ldarg.0
IL_0004: callvirt instance int32 [mscorlib]System.String::get_Length()
IL_0009: ldc.i4.0
IL_000a: ceq
IL_000c: ret
IL_000d: ldc.i4.1
IL_000e: ret
} // end of method Tester::MyNullOrEmpty

Willy.

"Andreas Håkansson" <andy.h (at) telia.com> wrote in message
Willy,

I've never actually went down the IL road (I really should), do you
mean
shortest as in "you wont have to create an class instance" or
shorter
 
Andreas said:
I added an external tool with

Command: C:\Program\Microsoft Visual Studio .NET
2003\SDK\v1.1\Bin\ildasm.exe
Arguments: $(TargetName)$(TargetExt)
Initial Directory: $(TargetDir)
[...]

I just use:

Commmand: C:\Program\Microsoft Visual Studio .NET
2003\SDK\v1.1\Bin\ildasm.exe
Arguments: $(TargetPath)
 
William Stacey said:
As an FYI, Empty is actually a public static string var set to Empty = "" in
the string classes static constructor. This should probably be faster then
testing Length == 0, but as you said, probably could not tell the difference
until you ran millions of times in a loop.

Why would it be faster? I could see how it could be faster in the cases
where you ended up with another reference to the same object as
String.Empty, but otherwise it's got to compare the strings for
equality somehow - and that's got to compare the lengths anyway.
 
James Hebben said:
I seem to recall that the FxCop tool (http://www.gotdotnet.com/team/fxcop/)
notes that the preferred way to test for an empty string is str.Length== 0
(plus all the Trim comments from previous posts of course ;-)

Indeed - the comment for the rule is:

<quote>
Constructs such as "".Equals(someString) and String.Empty.Equals
(someString) are less efficient than testing the string length. Replace
these with checks for someString.Length == 0.
</quote>
 
Your probably right. I was thinking it was comparing something like an int,
but did not think it through far enough. Either way, you probably need a
million or more cycles to measure any substantial difference.
 
Back
Top