Hi~~ About String.IsNullOrEmpty

Hi all:

If I want to make the rule: If a programmer want to test if a String variable is null or not, he should use String.IsNullOrEmpty instead of just typing "someone == null", so my question is: how to find the "someone == null" in the IL code...

For Example:

the method is:

public void testStringAdd()
{
String a = "a";
if (a == null)
{ }
}

could I find the "a == null"'s corresponding IL code? thank you~~~

[1108 byte] By [kevin1207] at [2007-12-28]
# 1

There isn't a single "a == null" IL pattern. The compiler can chose to implement this in many different ways. Off the top of my head I think "a == null" can look like a load (like a ldloc, ldarg, ldfld) and a ldnull followed by a ceq followed by a conditional branch or it can be as simple as load of the value you're testing followed by a conditional branch like brfalse or brtrue. Your best bet is looking for a load and a ldnull followed by a ceq or comparative branching instruction. I guess you could also determine if a load followed by a conditional branch is a nullness check by checking if the load is loading a reference type or a value type. Be warned that regardless, this is going to be an error prone check.

-Todd King

ToddKing-MSFT at 2007-9-4 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...

Visual Studio Team System

Site Classified