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 )

1 comment:

Need to say something ? Spell it out :)