Sunday, 13 February 2011

5 Wrong ways to check empty strings

It is one of the common mistake that people compare a string with “” or String.Empty in VB.Net or C# to find its empty. Here are few examples.
// C# Wrong Ways
  1. if ( s == “” )
  2. if ( s == string.Empty )
  3. if ( s.Equals(”") )
  4. if ( s.Equals ( String.Empty)
  5. if ( string.Equals(s,”")
  6. if ( string.Equals ( s,String.Empty ))
So what’s the correct way to do it ? Check for length too.
// [ C# ] Correct Way
if ( s.Length == 0 )

0 Responses to “5 Wrong ways to check empty strings”

Post a Comment

All Rights Reserved by Pro Hack . Copyright 2008 - 20011. Template by Bloggermint .