Skip to main content

How to Check Whether a String Is Null, Empty or Whitespace in C#

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A name field that holds only spaces is as empty as one that holds nothing, but value == "" treats it as filled in. string.IsNullOrWhiteSpace answers the real question in one call: is there any visible text at all?

Syntax​

Syntax
bool <isBlank> = string.IsNullOrWhiteSpace(<value>);
bool <isEmpty> = string.IsNullOrEmpty(<value>);
PartWhat it does
string.IsNullOrWhiteSpace(<value>)true when <value> is null, "", or nothing but white-space characters such as spaces.
string.IsNullOrEmpty(<value>)true only when <value> is null or "". A string of spaces counts as filled in.
<value>The string to check. It may be null; neither method throws for it.

Microsoft's String.IsNullOrWhiteSpace reference: "Indicates whether a specified string is null, empty, or consists only of white-space characters."

The Example​

The example checks four values with both methods: null, an empty string, three spaces, and the name Leo. Each line prints the value between square brackets, so you can see the spaces, followed by both results.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which loops over null, an empty string, three spaces and Leo, and prints each value in square brackets with the results of string.IsNullOrEmpty and string.IsNullOrWhiteSpace.

main.cs
string?[] values = [null, "", " ", "Leo"];

foreach (string? value in values)
{
Console.WriteLine($"[{value}] IsNullOrEmpty: {string.IsNullOrEmpty(value)}, IsNullOrWhiteSpace: {string.IsNullOrWhiteSpace(value)}");
}

string? says the array may hold null as well as strings.

Step 2: Run It​

Terminal
dotnet run main.cs
Output
[] IsNullOrEmpty: True, IsNullOrWhiteSpace: True
[] IsNullOrEmpty: True, IsNullOrWhiteSpace: True
[ ] IsNullOrEmpty: False, IsNullOrWhiteSpace: True
[Leo] IsNullOrEmpty: False, IsNullOrWhiteSpace: False

A zsh terminal where dotnet run main.cs prints the IsNullOrEmpty and IsNullOrWhiteSpace results for null, an empty string, three spaces and Leo.

The first two lines are null and "": both methods say True. (An interpolated string prints null as nothing, so both show [].) The third line is where they differ: three spaces aren't empty, so IsNullOrEmpty says False, but there's no visible text, so IsNullOrWhiteSpace says True. Leo is real text for both. The command is the same in zsh, bash and PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Checking value == ""null and " " both pass as filled inUse string.IsNullOrWhiteSpace(value)
Using IsNullOrEmpty for something a person typedA value of only spaces passes the checkUse IsNullOrWhiteSpace
Calling value.Trim() before the checkA null value throws NullReferenceExceptionCall string.IsNullOrWhiteSpace(value), which accepts null

Troubleshooting​

SymptomLikely causeFix
A blank-looking value passes validationIt holds spaces, and the check is IsNullOrEmpty or == ""Switch to IsNullOrWhiteSpace
NullReferenceException in a checkA method was called on a null stringUse the static string.IsNullOrWhiteSpace

FAQ​

Which one should I use?​

IsNullOrWhiteSpace for anything a person types, such as names and search terms, where spaces alone mean nothing was entered. IsNullOrEmpty when spaces are real content, such as a separator.

Is IsNullOrWhiteSpace the same as trimming and checking the length?​

Almost. The reference calls it "a convenience method that is similar to the following code, except that it offers superior performance":

From the reference
return String.IsNullOrEmpty(value) || value.Trim().Length == 0;

What counts as white space?​

More than the space bar. The reference: "White-space characters are defined by the Unicode standard." Tabs and line breaks count too, so a value of only tabs or only newlines is also treated as blank.

Conclusion​

string.IsNullOrWhiteSpace(value) is true for null, "" and strings of only spaces, and false as soon as there's visible text. IsNullOrEmpty stops at null and "". Both are static and safe to call with null.

Additional Resources​