Partilhar via


New Method: String.IsNullOrWhiteSpace()

In .NET 2.0 one of the most useful (but simplest) added methods was String.IsNullOrEmpty(). This method allowed developers to replace thousands of instances of (variable == null || variable == String.Empty) with (String.IsNullOrEmpty(variable)). Not only is this shorter it also expresses intent better.

The big limitation of String.IsNullOrEmpty() is that in some circumstances you needed a broader interpretation of “empty” that also encompassed strings that consist entirely of whitespace. Well, not to be outdone, .NET 4.0 introduces String.IsNullOrWhiteSpace(). While I haven’t investigated exactly what this method considers whitespace I assume it is a string consisting entirely of characters where Char.IsWhiteSpace(char) returns true.