C# string.IsNullOrEmpty Method

Dot Net Perls
String type

You want to use the string.IsNullOrEmpty method to check for string references that are null, or empty strings. Strings are reference types and can be equal to the null value like any other reference type. Strings can also be empty, meaning their values equal "" and they have zero length.

Performance test for string.IsNullOrEmpty

Version 1: 733 ms
Note:      Checks string for null, and then checks against "".

Version 2: 546 ms
Note:      Calls string.IsNullOrEmpty static method in framework.

Version 3: 406 ms
Note:      Checks string for null, and then check Length.

Example

Here we look at several examples of the IsNullOrEmpty static method on the string type in the C# language. The three parts in the code sample show the IsNullOrEmpty method matching a null string, an empty string, and then a normal string. The parts A and B in the example will match, but C won't.

Program that uses string.IsNullOrEmpty [C#]

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
Note (please read)

IsNullOrEmpty detects two conditions. In the C# language, strings can be either equal to the null literal, meaning they don't reference any data, or empty, meaning they reference a character buffer is 0 characters long. IsNullOrEmpty detects both of these conditions at the same time.

Null Tips

The method is static. It is a static method because you may want to call it on a null string. Using an instance method would cause the NullReferenceException to be raised.

NullReferenceException and Null Parameter

The method returns bool. It returns true or false. This indicates whether the string is empty or null. In other words, if it has something substantial in it, the result bool will be false.

Bool Type

Alternative methods

Here are three ways of doing the same thing that IsNullOrEmpty does. Note how the string is tested against null before checking its length. This avoids a null reference.

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 of the benchmark. What I found is that string.IsNullOrEmpty is about in the middle in performance. However, the code for it is much clearer, which is a huge positive. You can see the results of the benchmark at the top of this article.

Summary

The C# programming language

We saw the IsNullOrEmpty method on the C# string type, which provides us with a good and relatively efficient method of checking whether a string is OK to save or use. However, for performance, it may be better to use manual null checks. Empty strings can also be tested in other ways, and my research here shows that checking length is fastest.

Empty String Examples String Type