Is Null Or White Space

null

So I did not know about IsNullOrWhiteSpace! Did you?

I have of course been using IsNullOrEmpty for years which does exactly what you would imagine, but I was recently pulling info from a config file where I needed to account for the presence of white spaces. IsNullOrWhiteSpace does the equivalent of the following line of code but much more efficiently:

return string.IsNullOrEmpty(somevalue) || somevalue.Trim().Length == 0;

All white-space characters are defined by the Unicode standard, so the IsNullOrWhiteSpace method interprets any character that returns a value of true when it is passed to the Char.IsWhiteSpace method as a white-space character.

I am finding this a good alternative to an null-empty check in some situations, now I am trying to replace or all the redundant code in my open source project.



Comment Section

Comments are closed.