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
- if ( s == “” )
- if ( s == string.Empty )
- if ( s.Equals(””) )
- if ( s.Equals ( String.Empty)
- if ( string.Equals(s,””)
- 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 )
This is in continuation of our last post on Check for length too when you check for null strings in VB.Net and C#
Read below for the right approach.