Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

C# Dictionary

  1. Home
  2. C# Dictionary
  3. string.IsNullOrEmpty() / string.IsNullOrWhiteSpace()

string.IsNullOrEmpty() / string.IsNullOrWhiteSpace()

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

// Returns true if the string is null or "".
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

MemberDescription
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

using System;

// How to use IsNullOrEmpty().
string? name1 = null;
string? name2 = "";
string? name3 = "Tanaka";

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

// 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 = "Dark";
string theme = setting ?? "Light";
Console.WriteLine(theme); // Dark (not null, so the original value is used)

// Example of the ??= operator.
string? cache = null;
cache ??= "initial value";
Console.WriteLine(cache); // initial value

Notes

When validating form input, you need to account for users entering only spaces. In such cases, it is recommended to prefer IsNullOrWhiteSpace() over IsNullOrEmpty().

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 .