C# String IsNullOrWhiteSpace() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In C#, string.IsNullOrWhiteSpace() returns true when a string has no visible content: it is null, it is "", or every character in it is white space (spaces, tabs, line breaks, and other char.IsWhiteSpace characters).

It is the natural check for user input. A user who presses the space bar and then Enter has technically typed something, but not anything useful. Like IsNullOrEmpty(), it is static and safe to call with null.

This is a static member of System.String. Call it on the type, such as string.IsNullOrWhiteSpace(...), not on a string variable. It only reads the text. Strings are immutable, so the original string is never changed. It is available in .NET Framework 4.0 and later, and in all versions of .NET Core and .NET.

Syntax

Pass the string you want to test.

C#
string input = "   ";

if (string.IsNullOrWhiteSpace(input)) {
    Console.WriteLine("Please type something");
}
CallWhat it does
string.IsNullOrWhiteSpace(text)true for null, "", or only white space.
!string.IsNullOrWhiteSpace(text)true when there is at least one visible character.

It gives the same answer as string.IsNullOrEmpty(text?.Trim()), but it never creates a trimmed copy; it just scans the characters and stops at the first non-white-space one. That makes it both shorter and cheaper.

Library declarations
C#
public static bool IsNullOrWhiteSpace(string? value);

Parameters

ParameterDescription
valueRequired. The string to test. It may be null.

Return Value

Return ValueDescription
truevalue is null, "", or contains only white-space characters.
falseAt least one character is not white space.

C# Examples

Example 1 — Blank or Not? Five Test Values

Check null, empty, spaces, a tab with a newline, and one letter.

C#
using System;

class Program {
    static void Main() {
        string?[] values = { null, "", "   ", "\t\n", "a" };

        foreach (string? v in values) {
            Console.WriteLine(string.IsNullOrWhiteSpace(v));
        }
    }
}

How It Works

  1. An array that allows null. string?[] tells the compiler that some entries may be null.
  2. Missing and empty. The first two values are True, exactly as with IsNullOrEmpty.
  3. Invisible characters. Three spaces, and a tab followed by a newline, are both treated as blank, so they print True as well.
  4. One letter is enough. "a" has a visible character, so the last result is False.

Example 2 — Reject a Name Made of Spaces

Compare the two static checks on spaces-only input, then show the message a form would display.

C#
using System;

class Program {
    static void Main() {
        string input = "   ";

        Console.WriteLine(string.IsNullOrEmpty(input));
        Console.WriteLine(string.IsNullOrWhiteSpace(input));

        if (string.IsNullOrWhiteSpace(input)) {
            Console.WriteLine("Please enter your name");
        }
    }
}

How It Works

  1. Three spaces. This is what a form receives when someone taps the space bar and submits.
  2. IsNullOrEmpty lets it through. The string has length 3, so the first check says it is not empty.
  3. IsNullOrWhiteSpace catches it. Every character is a space, so the second check returns True.
  4. Show a helpful message. The if uses the stricter check and asks the user to try again.

Example 3 — Keep Only Lines with Real Content

Filter a list of lines and count how many survive.

C#
using System;

class Program {
    static void Main() {
        string[] lines = { "first", "", "   ", "second", "\t" };
        int kept = 0;

        foreach (string line in lines) {
            if (!string.IsNullOrWhiteSpace(line)) {
                Console.WriteLine(line);
                kept++;
            }
        }

        Console.WriteLine("Kept " + kept + " of " + lines.Length);
    }
}

How It Works

  1. Messy input. Text files often contain empty lines and lines with only spaces or tabs.
  2. Negate to keep. !string.IsNullOrWhiteSpace(line) is true only for lines that have something visible.
  3. Count as you go. kept grows by one for each printed line.
  4. Three blanks dropped. Out of five lines, the empty one, the spaces one, and the tab one are skipped.

Frequently Asked Questions

It returns true when the string is null, empty, or made only of white-space characters such as spaces, tabs, and line breaks.
Use IsNullOrWhiteSpace for user input and text files, where blank values often contain spaces. Use IsNullOrEmpty when spaces are meaningful content.
No. It only checks. Call Trim() afterwards if you want to clean the value.
.NET Framework 4.0 and later, and every version of .NET Core and .NET 5+.
Slightly, because it may scan characters. It does not create any new strings, so it is still very fast and much cheaper than trimming first.

Next: Join()

Glue many strings (or values) into one, with a separator between each.

Join() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful