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. Here we see some examples and benchmarks of string.IsNullOrEmpty in the C# language.
=== Performance test for string.IsNullOrEmpty in C# === IsNullOrEmpty was not the fastest way to check strings. Based on .NET 3.5 SP1. 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.
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 of the program ===
Null or empty
Null or empty
Not null and not emptyIsNullOrEmpty 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.
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.
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.
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.
Here we looked that IsNullOrEmpty method on the 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.