Coding Geek

A blog by Nadeem Iqbal

C# 4.0 - New Feature I Like Most

String.IsNullOrWhiteSpace

Previous version of C# contain method String.IsNullOrEmpty which checks whether string is empty or null. If String only contain white spaces (it's also a case of empty string) you will be required to add an additional trim() function to handle it. But if you will use trim() on an null string, it will throw exception so you will be required to write few lines of code for it. But new function in string class

The new String.IsNullOrWhiteSpace method indicates whether a string is null, empty, or consists only of white-space characters.


Posted: Jul 23 2010, 04:06 PM by niqbal | with 4 comment(s) |
Filed under: , , , ,

Comments

Steve said:

That is handy however I've been doing something similar to this using extension methods for a while now.

if (myVariable.IsEmpty()) { //extension merthod

//do something

}

# July 23, 2010 7:14 AM

Sean Patterson said:

Nice to know they are continuing the progression. I've found IsNullOrEmpty immensely helpful, especially when parsing web forms.

Back in the 1.0/1.1 days, I was so tired of doing all the nested if logic that I wrote my own "helper" function called "IsSomething" and just validated things that way. 8^D

# July 23, 2010 12:52 PM

rlambda said:

Nice feature... thanks for sharing the good information

# July 24, 2010 9:21 AM

Jinky said:

This is not a C# feature, it is a .NET BCL feature.  It is available in any .NET language.  C# 4.0 != .NET 4.0 (you can use C# 4.0 features like optional parameters when writing code for previous versions of the .NET framework)

# July 26, 2010 10:48 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)