C#:String

.NET Array Dictionary List String 2D Async DataTable Dates DateTime Enum File For Foreach Format IEnumerable If IndexOf Lambda Parse Path Process Property Regex Replace Row Sort Split Static StringBuilder Substring Switch Tuple Window

IsNullOrEmpty. The string.IsNullOrEmpty method tests strings. It checks for string references that are null or have no data. Strings are reference types and can be equal to null.
Strings can also be empty—of zero length. An empty string is just empty, not null. But we cannot check the first char of an empty string. An IndexOutOfRangeException will occur.Empty Strings
Here, we look at examples of IsNullOrEmpty. The three parts in the code sample show the IsNullOrEmpty method matching a null string, an empty string, and then a normal string.Null StringsString Literal

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

Based on: .NET 4

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. 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. I found 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.
A summary. IsNullOrEmpty, a string method, provides us with a way to check whether a string is okay to save or use. For performance it may be better to use manual null checks.