Nurasto.com Website

My digital scrapbook

String.IsNullOrEmpty Flaw

on under Uncategorized • 2 Comments

Please take your time to visit Bill’s blog about String.IsNullOrEmpty Flaw. I am rewrite his code into new C# console project, the code is

class Program
{  static void Main(string[] args)
  {
   Console.WriteLine(“Starting”);
   test(
null
);
   Console.WriteLine(“Finished”
);
   Console
.ReadLine();
  }
  static void test(string x)
 
{
 
   for (int i = 0; i < 10; i++)
    {
  
   if (String.IsNullOrEmpty(x))
      {
        
//TODO
     
}
   
}
   }
}

Now, build the code and set configurastion on Configuration Manager as “Release”. Try running the compiled .EXE and you will get the System.NullReferenceException.

Ok, let’s replace the commented //TODO into something meaningfull like

Console.WriteLine(“This is null string”);

What you get? did you get error or not?. If not the we have same situation. In my conclusion the empty IF block caused an  Exception. I rarely use String.IsNullOrEmpty on a loop, but this case make me aware to use this method with heart.

I read that JIT team will not fix it until Post-Orcas is roll out … upz

Tag: , ,