String.IsNullOrEmpty Flaw

Posted by Nurasto | June 19, 2007 | 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: , ,

Comments

  1. Willy Sudiarto Raharjo says:

    re: String.IsNullOrEmpty Flaw

    Strange, i sometimes used that too (but not TODO string, but some code which i commented out since i want to debug it) and it worked.

  2. Dityo Nurasto says:

    re: String.IsNullOrEmpty Flaw

    Yes, it’s a strange behavior. I still don’t understand why this could be happen. Most of case is not going to throw an exception.

Sorry, comments are closed

Bit and Bytes

Hello. My name is Dityo Nurasto. I am working as freelance software and web developer.

This is my personal playground. Enjoy your stay and don't hesitate to send comments.

ShoutBox