IsNullOrEmpty vs IsNullOrWhiteSpace
Are you also thinking , is IsNullOrEmpty better or IsNullOrWhiteSpace better?
The answer is IsNullOrWhiteSpace, unless you want to accept space (" ") or spaces(" ") as valid input.
The reason is IsNullOrWhiteSpace will validate (" ") as true, while IsNullOrEmpty required you to trim the string inorder to validate it the empty space. If you trim a string that is null, you will get exception.
Before the introduce of IsNullOrWhiteSpace , the usual way checking might be:
if(!string.IsNullOrEmpty(stringA))
if (stringA.Trim().Length != "")
dataSavingForExample;
IsNullOrWhiteSpace : lesser code, safer for empty space checking and better performance. :)
No comments:
Post a Comment