In all my books and conference sessions I talk about the proper way to test if a string is valid. Microsoft .NET has been around almost two decades and I still see code like this,
- if (testValue.Trim() == "")
This code is even wrong since it's not checking for null. A better way would be like this,
- if (testValue == null || testValue.Trim() == "")
While this is valid code, it IS a performance issue. Strings should always be checked using string.IsNullOrEmpty() (the one I use the most) or string.IsNullOrWhiteSpace(). Here is an example,
- if (string.IsNullOrEmpty(testValue))
In my testing in .NET Core, using IsNullOrEmpty is 95% faster and in .NET Framework 4.7.1 it's 93% faster!

Mohamad DboukPosted Sep 1, 2018, 12:08 PM
Can you give us the statistics of using string.IsNullOrWhiteSpace? Also, why you are using string.IsNullOrEmpty the most?
Sundaram SubramanianPosted Aug 23, 2018, 1:38 PM
A good information. I also use the same way as you do.
Jignesh KumarPosted Aug 22, 2018, 8:59 PM
Nice one thank you