C# String IsNullOrEmpty() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

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 null into 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";
}
CallWhat 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

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

Return Value

Return ValueDescription
truevalue is null or "".
falsevalue 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));
    }
}

How It Works

  1. No string at all. a is null; the ? on string? tells the compiler that is allowed. The check returns True without crashing.
  2. Zero characters. "" is a real string with length 0, which also counts as empty.
  3. One space is something. " " has length 1, so the result is False.
  4. 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"));
    }
}

How It Works

  1. A guard clause. The first lines of Greet handle the missing case and leave early with return.
  2. Both kinds of missing. null and "" follow the same path, so the caller does not need to care which one it has.
  3. Normal path. With a real name, the check is false and the greeting uses it.
  4. Static call. Notice the call is string.IsNullOrEmpty(name); nothing is ever called on name 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));
    }
}

How It Works

  1. Nothing to ask. s does not point to any string, so s.Length has no object to read from and throws NullReferenceException.
  2. A compiler hint. With nullable reference types on, the compiler warns about s.Length here. The warning is right: this line is a bug.
  3. The safe version. string.IsNullOrEmpty(s) receives the null as an argument and simply returns True.
  4. 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.

Next: IsNullOrWhiteSpace()

Check whether a string is null, empty, or made only of spaces, tabs, and line breaks.

IsNullOrWhiteSpace() 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.

13 people found this page helpful