Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

Empty strings in C#

Following on from the thread about empty strings in Visual Basic .NET, how should I check for empty strings in C#?

strName == "" ?
strName == null ?
strname == String.Empty ?

I'm new to C# and this is something that's confused me.


Thursday, June 24, 2004


Thursday, June 24, 2004

You do understand that comparing to null and string.Empty are different right?

bool IsNullOrEmpty(string str)
  return str==null || str.Length==0;

Duncan Smart
Friday, June 25, 2004

Duncan, I think he's asking because we just learned in the other thread that VB.NET actually treats Nothing (null) and the empty string as the same thing.

Chris Nahr
Friday, June 25, 2004

That's sort of misleading.

If you cast Nothing to a string, it'll become the empty string. But if you use it properly (i.e., "is nothing" instead of "= nothing"), then they are distinct.

Brad Wilson (
Saturday, June 26, 2004

I've also used the "if (str==null || str.Length==0) approach.

Mark Hoffman
Monday, June 28, 2004

*  Recent Topics

*  Fog Creek Home