string.IsNullOrEmpty() / string.IsNullOrWhiteSpace()
| Since: | IsNullOrEmpty() | C# 2.0(2005) |
|---|---|---|
| IsNullOrWhiteSpace() | C# 4.0(2010) |
Three tools for handling null and empty strings in C#: string.IsNullOrEmpty() checks for null or an empty string, string.IsNullOrWhiteSpace() also detects whitespace-only strings, and the ?? operator returns a fallback value when the left-hand side is null.
Syntax
string.IsNullOrEmpty(string? value) // Returns true if the string is null, "", or contains only whitespace (spaces, tabs, etc.). string.IsNullOrWhiteSpace(string? value) // Returns the right-hand value if the left-hand side is null (null-coalescing operator). left ?? right // Assigns the right-hand value to the variable only if it is null (null-coalescing assignment operator). variable ??= value
Member List
| Member | Description |
|---|---|
| string.IsNullOrEmpty(string? value) | Returns true if value is null or a string of length 0. |
| string.IsNullOrWhiteSpace(string? value) | Returns true if value is null, an empty string, or contains only whitespace characters (spaces, tabs, newlines). |
| ?? (null-coalescing operator) | Returns the left-hand side if it is not null; otherwise returns the right-hand side. |
| ??= (null-coalescing assignment operator) | Assigns the right-hand side to the variable only if the variable is null. |
Sample Code
Program.cs
using System; // How to use IsNullOrEmpty() string? name1 = null; string? name2 = ""; string? name3 = "Son Goku"; Console.WriteLine(string.IsNullOrEmpty(name1)); // True Console.WriteLine(string.IsNullOrEmpty(name2)); // True Console.WriteLine(string.IsNullOrEmpty(name3)); // False // IsNullOrWhiteSpace() also detects strings that contain only spaces string? input = " "; // Whitespace only Console.WriteLine(string.IsNullOrEmpty(input)); // False (IsNullOrEmpty does not catch this) Console.WriteLine(string.IsNullOrWhiteSpace(input)); // True
This produces the following output:
dotnet run True True False False True
?? and ??= Operators
The null-coalescing operator (??) provides a concise way to specify a default value when a result is null.
NullCoalescing.cs
using System; // Use the ?? operator to provide a default value when the result is null string? username = null; string displayName = username ?? "Guest"; Console.WriteLine(displayName); // Guest string? setting = "Vegeta"; string selected = setting ?? "Default"; Console.WriteLine(selected); // Vegeta (not null, so the original value is used) // Example of the ??= operator string? cache = null; cache ??= "initial value"; Console.WriteLine(cache); // initial value // Chained ?? operators string? a = null; string? b = null; string? c = "Frieza"; string result = a ?? b ?? c ?? "none"; Console.WriteLine(result); // Frieza
This produces the following output:
dotnet run Guest Vegeta initial value Frieza
Practical Pattern: Validation
An example of using these methods for form input validation.
Validation.cs
using System;
void Validate(string? value) {
if (string.IsNullOrWhiteSpace(value)) {
Console.WriteLine("Input is empty.");
return;
}
Console.WriteLine($"Input: {value.Trim()}");
}
Validate(null); // Input is empty.
Validate(""); // Input is empty.
Validate(" "); // Input is empty.
Validate(" Son Goku "); // Input: Son Goku
This produces the following output:
dotnet run Input is empty. Input is empty. Input is empty. Input: Son Goku
Common Mistakes
Common Mistake: IsNullOrEmpty() Cannot Detect Whitespace-Only Strings
If a user enters only spaces in a form, IsNullOrEmpty() returns false and validation will pass unexpectedly.
using System;
// NG: IsNullOrEmpty() cannot detect a whitespace-only string
string? userInput = " "; // Whitespace only
if (string.IsNullOrEmpty(userInput)) {
Console.WriteLine("No input."); // Not reached
} else {
Console.WriteLine("Input: " + userInput); // This branch executes
}
This produces the following output:
dotnet run Input:
using System;
// OK: Use IsNullOrWhiteSpace()
string? userInput = " ";
if (string.IsNullOrWhiteSpace(userInput)) {
Console.WriteLine("No input.");
} else {
Console.WriteLine("Input: " + userInput);
}
This produces the following output:
dotnet run No input.
Notes
When validating form input, you need to account for users entering only spaces. In such cases, using IsNullOrWhiteSpace() instead of IsNullOrEmpty() is more appropriate.
The ?? operator lets you simplify null-check if statements and is commonly used to set default values. For details on nullable types (string?), refer to the C# null-safety design specification.
For type conversion utilities, see Convert.ToString() / Convert.ToInt32().
If you find any errors or copyright issues, please contact us.