Code It Any Way You Want: Checking Strings for Null

How do you check a string for null? Well, there is very little difference between these coding methods.

if (testString == null)

or

if (testString is null)

or

if (string.IsNullOrEmpty(testString))

Just remember that IsNullOrEmpty() also checks for an empty string, so my recommendation is to utilize it.

Benchmark Results

Below are the benchmark results for these null checks. All methods allocate zero bytes in memory.

Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks

If you liked this article, please buy David a cup of Coffee by going here: https://www.buymeacoffee.com/dotnetdave

© The information in this article is copywritten and cannot be reproduced in any way without express permission from David McCarter.


Discover more from dotNetTips.com

Subscribe to get the latest posts sent to your email.

Leave a Reply