In C#, string.IsNullOrEmpty() answers one question: does this string have no text at all? It returns true when the value is null (no string) or"" (a string with zero characters). Otherwise it returns false.
It is static, so you call it as string.IsNullOrEmpty(name), not name.IsNullOrEmpty(). That is the whole point: calling any instance method on a null variable would crash, but passing nullinto a static method is safe. A string of only spaces, like " ", is not empty; for that use IsNullOrWhiteSpace().
This is a static member of System.String. Call it on the type, such as string.IsNullOrEmpty(...), not on a string variable. It only reads the text. Strings are immutable, so the original string is never changed.
Syntax
Pass the string you want to test.
C#
string? name = null;
if (string.IsNullOrEmpty(name)) {
name = "guest";
}
Call
What it does
string.IsNullOrEmpty(text)
true for null or "".
!string.IsNullOrEmpty(text)
true when there is at least one character.
String.IsNullOrEmpty(text)
Same call; string and String are the same type.
It is the same as writing text == null || text.Length == 0, in the safe order. The || stops early when text is null, so Length is never read on a missing string. With nullable reference types (C# 8+), the compiler knows the value is not null after a false result.
Library declarations
C#
public static bool IsNullOrEmpty(string? value);
Parameters
Parameter
Description
value
Required. The string to test. It may be null.
Return Value
Return Value
Description
true
value is null or "".
false
value has one or more characters (even if they are only spaces).
C# Examples
Example 1 — Test Four Kinds of Strings
Run the check on null, an empty string, a single space, and a real word.
C#
using System;
class Program {
static void Main() {
string? a = null;
string b = "";
string c = " ";
string d = "hi";
Console.WriteLine(string.IsNullOrEmpty(a));
Console.WriteLine(string.IsNullOrEmpty(b));
Console.WriteLine(string.IsNullOrEmpty(c));
Console.WriteLine(string.IsNullOrEmpty(d));
}
}
Output
True
True
False
False
How It Works
No string at all.a is null; the ? on string? tells the compiler that is allowed. The check returns True without crashing.
Zero characters."" is a real string with length 0, which also counts as empty.
One space is something." " has length 1, so the result is False.
Real text."hi" is clearly not empty.
Example 2 — Use a Default When a Name Is Missing
A small method greets the user, falling back to guest when no name was given.
C#
using System;
class Program {
static string Greet(string? name) {
if (string.IsNullOrEmpty(name)) {
return "Hello, guest";
}
return "Hello, " + name;
}
static void Main() {
Console.WriteLine(Greet(null));
Console.WriteLine(Greet(""));
Console.WriteLine(Greet("Sam"));
}
}
Output
Hello, guest
Hello, guest
Hello, Sam
How It Works
A guard clause. The first lines of Greet handle the missing case and leave early with return.
Both kinds of missing.null and "" follow the same path, so the caller does not need to care which one it has.
Normal path. With a real name, the check is false and the greeting uses it.
Static call. Notice the call is string.IsNullOrEmpty(name); nothing is ever called onname itself.
Example 3 — Why Not Just Check Length?
Reading Length on a null string crashes. The static check does not.
C#
using System;
class Program {
static void Main() {
string? s = null;
try {
bool empty = s.Length == 0;
Console.WriteLine(empty);
} catch (NullReferenceException ex) {
Console.WriteLine(ex.GetType().Name);
}
Console.WriteLine(string.IsNullOrEmpty(s));
}
}
Output
NullReferenceException
True
How It Works
Nothing to ask.s does not point to any string, so s.Length has no object to read from and throws NullReferenceException.
A compiler hint. With nullable reference types on, the compiler warns about s.Length here. The warning is right: this line is a bug.
The safe version.string.IsNullOrEmpty(s) receives the null as an argument and simply returns True.
Another safe spelling.s?.Length == 0 avoids the crash too, but it returns false for null, which is easy to get wrong. The static method says exactly what you mean.
Frequently Asked Questions
It returns true when the string is null or has zero characters (""), and false otherwise.
Because it must work when the string is null. You cannot call an instance method on a null variable without getting NullReferenceException, but you can pass null to a static method.
No. " " has three characters, so IsNullOrEmpty returns false. Use IsNullOrWhiteSpace to treat spaces as blank.
IsNullOrEmpty is true only for null and "". IsNullOrWhiteSpace is also true for strings made only of white space, such as " " or "\t\n".
Yes, it gives the same result. It is shorter, clearer, and the order of the checks is always correct.