C# string.IsNullOrEmpty, IsNullOrWhiteSpace

Use the string.IsNullOrEmpty method to test strings. Also call IsNullOrWhiteSpace.
IsNullOrEmpty, IsNullOrWhiteSpace. The string.IsNullOrEmpty method tests strings. It checks for string references that are null or have no data. IsNullOrWhiteSpace tests for whitespace.
Strings are reference types and can be equal to null. Often our programs want to handle empty, null, or whitespace strings in special ways. These 2 methods are ideal here.Empty String
IsNullOrEmpty. Here, we look at examples of IsNullOrEmpty. The 3 parts in the code sample show the IsNullOrEmpty method matching a null string, an empty string, and then a normal string.Null StringsString LiteralStrings

Important: We cannot check the first char of an empty string. An IndexOutOfRangeException will occur.

Next: In this program, the parts A and B in the example will match, but part C won't.

C# program that uses string.IsNullOrEmpty using System; class Program { static void Main() { // A. // Test with IsNullOrEmpty. string s = null; if (string.IsNullOrEmpty(s) == true) { // True. Console.WriteLine("Null or empty"); } else { Console.WriteLine("Not null and not empty"); } // B. // Test with IsNullOrEmpty again. string y = ""; if (string.IsNullOrEmpty(y) == true) { // True. Console.WriteLine("Null or empty"); } else { Console.WriteLine("Not null and not empty"); } // C. // Test with IsNullOrEmpty without writing 'true'. string z = "sam"; if (string.IsNullOrEmpty(z)) { // False. Console.WriteLine("Null or empty"); } else { Console.WriteLine("Not null and not empty"); } } } Output Null or empty Null or empty Not null and not empty
IsNullOrEmpty detects two conditions. Strings can equal null, meaning they don't reference any data. They can be empty, meaning they reference a character buffer that is 0 characters long.Null
Static method. IsNullOrEmpty detects both conditions at the same time. It is static. You may want to call it on a null string. An instance method would cause a NullReferenceException.StaticNullReferenceException

Return: IsNullOrEmpty returns bool—either true or false. This indicates whether the string is empty or null.

So: In other words, if the string has something substantial in it, the result bool will be false.

BoolReturn Bool
Internals, IsNullOrEmpty. How is IsNullOrEmpty implemented? It simply tests for null and then a Length equal to 0. It provides no magic optimizations (as testing also shows).
Alternatives. We compare alternative ways of doing these checks. Please note how the string is tested against null before checking its length. This avoids a null reference exception.
Version 1: C# // Test version 1. if (s == null || s == "") { Console.WriteLine("1"); } Version 2: C# // Test version 2. if (string.IsNullOrEmpty(s)) { Console.WriteLine("2"); } Version 3: C# // Test version 3. if (s == null || s.Length == 0) { Console.WriteLine("3"); }
Results. We find that string.IsNullOrEmpty is about in the middle in performance. However, the code for it is much clearer, which is a huge positive.
Version 1: 733 ms Checks string for null, and then checks against "". Version 2: 546 ms Calls string.IsNullOrEmpty static method in framework. Version 3: 406 ms Checks string for null, and then check Length.
IsNullOrWhiteSpace. This method tests an entire string. It returns true if the string contains only whitespace characters or is null.

Example: Whitespace characters in C# programs can be encoded as \n for new lines, \r for carriage returns, or even \v for vertical spaces.

And: Not only this, but whitespaces can include regular spaces and tab characters \t.

Note: IsNullOrWhiteSpace handles all these cases. It scans the entire input string if the string has more than one whitespace character.

Tip: The string must be entirely whitespace chars or null for the result to be true.

C# program that uses IsNullOrWhiteSpace using System; class Program { static void Main() { bool a = string.IsNullOrWhiteSpace(" "); bool b = string.IsNullOrWhiteSpace("\n"); bool c = string.IsNullOrWhiteSpace("\r\n\v "); bool d = string.IsNullOrWhiteSpace(null); bool e = string.IsNullOrWhiteSpace(""); bool f = string.IsNullOrWhiteSpace(string.Empty); bool g = string.IsNullOrWhiteSpace("dotnetperls"); Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine(c); Console.WriteLine(d); Console.WriteLine(e); Console.WriteLine(f); Console.WriteLine(g); } } Output True True True True True True False
Internals, IsNullOrWhiteSpace. The method first branches on the null comparison to the string variable. Next, if non-null, it scans the individual characters and calls IsWhiteSpace.

Also: The IsNullOrWhiteSpace method returns early if it finds a non-whitespace character.

Implementation in .NET Framework: C# public static bool IsNullOrWhiteSpace(string value) { if (value != null) { for (int i = 0; i < value.Length; i++) { if (!char.IsWhiteSpace(value[i])) { return false; } } } return true; }
Performance, IsNullOrWhiteSpace. The method calls result in a small performance penalty. IsWhiteSpace has some indirections that can be removed if you are not using Unicode data.

So: Using custom methods to test for whitespace may be faster in micro-benchmarks (and some real programs too).

A summary. IsNullOrEmpty, a string method, gives us a way to check whether a string is acceptable to save or use. For performance it may be better to use manual null checks.
Review, methods. The string.IsNullOrWhiteSpace method provides a handy and efficient way to determine some information about a string in your program.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to
Dot Net Perls