Question

C# Empty Strings: string.Empty, String.Empty, or ""?

csharp.netstringinitialization

Question

In C#, I need to initialize a string with an empty value. Which form should I use, and why?

string willi = string.Empty;
string willi = String.Empty;
string willi = "";

Are these forms different, and is one considered the preferred approach?

Short Answer

You will learn that string.Empty, String.Empty, and "" all represent an empty, non-null C# string. You will also learn why string and String are equivalent, when an empty string differs from null, and which style is commonly preferred.

Concept

An empty string is a string with zero characters:

string name = "";

It is not the same as null. An empty string exists and can safely use string members such as .Length; null means no string object is present.

All of these produce an empty string:

string a = "";
string b = string.Empty;
string c = String.Empty;

string is the C# keyword alias for the .NET type System.String. Therefore, string.Empty and String.Empty refer to the same static field:

string text = string.Empty;
String otherText = String.Empty;

In most C# codebases, "" is the most common choice when assigning an empty string directly because it is short and immediately recognizable. string.Empty is also correct and can be useful when it reads better in a particular expression. There is no meaningful performance reason to prefer one over the other in normal application code.

Choose one style and follow your team's conventions consistently.

Mental Model

Think of a string as a labelled envelope containing text.

  • "hello" is an envelope containing five characters.
  • "" is a real envelope containing no characters.
  • null means there is no envelope at all.

string.Empty and String.Empty are simply named ways of referring to the same empty envelope as "".

Also think of string and String as two names for the same type:

  • string is C#'s built-in nickname.
  • String is the .NET type name, short for System.String.
Take Quiz

Syntax and Examples

Use an empty string literal for a direct initialization:

string firstName = "";

You can also use the named field:

string firstName = string.Empty;

Both values have zero characters:

Console.WriteLine(firstName.Length); // 0
Console.WriteLine(firstName == "");  // True

String.Empty is equivalent, but it uses the .NET type name:

String firstName = String.Empty;

C# style commonly favors the keyword aliases string, int, and bool over String, Int32, and Boolean when writing normal code:

string message = "";
int count = 0;
bool isReady = false;

If a value may be absent, use a nullable string rather than pretending absence is an empty value:

string? middleName = null;

Step by Step Execution

Consider this code:

string message = "";

Console.WriteLine(message.Length);

message += "Hello";

Console.WriteLine(message);
Console.WriteLine(string.IsNullOrEmpty(message));

Step by step:

  1. message is initialized to an empty, non-null string.
  2. message.Length is 0, so the first line prints 0.
  3. message += "Hello" creates and assigns a new string containing Hello. Strings are immutable, so the original value is not modified in place.
  4. The next line prints Hello.
  5. string.IsNullOrEmpty(message) returns false because message now contains characters.

If message had stayed as "", string.IsNullOrEmpty(message) would return true.

Real World Use Cases

Empty strings are useful when an application has a known text value that currently contains no characters.

  • Form fields: Initialize a search box, display name, or optional note before the user enters text.
  • String building: Start with an empty result when joining small pieces of text.
  • API output: Return "" when a method promises to return a non-null string but has no text to return.
  • Default configuration: Use an empty prefix or suffix when a feature is optional.
  • Data cleanup: Replace missing text with an empty string when the business rule requires a usable, non-null value.

Example:

string greeting = "";

if (isMember)
{
    greeting = "Welcome back!";
}

Console.WriteLine(greeting);

Whether an empty string is appropriate depends on meaning. For a missing database value, null may preserve more information than converting it to "".

Take Quiz

Real Codebase Usage

In production code, developers usually focus less on the spelling of the empty string and more on clearly handling empty, whitespace-only, and null input.

A guard clause can reject invalid text early:

public static string NormalizeDisplayName(string? name)
{
    if (string.IsNullOrWhiteSpace(name))
    {
        return "";
    }

    return name.Trim();
}

Use string.IsNullOrEmpty when whitespace is allowed but a null or zero-length string is not:

if (string.IsNullOrEmpty(apiKey))
{
    throw new InvalidOperationException("An API key is required.");
}

Use string.IsNullOrWhiteSpace for user-entered values when spaces alone should count as empty:

if (string.IsNullOrWhiteSpace(email))
{
    return "Email is required.";
}

For a non-null property, initialize it directly:

public class Profile
{
    public string Biography { get; set; } = "";
}

This is particularly useful with nullable reference types enabled, because it makes the property's non-null intent explicit.

Common Mistakes

Confusing null and an empty string

This code throws a NullReferenceException because text is null:

string? text = null;
Console.WriteLine(text.Length); // Broken

An empty string is safe to inspect:

string text = "";
Console.WriteLine(text.Length); // 0

Using == "" when null is possible

This check does not treat null as empty:

string? input = null;

if (input == "")
{
    Console.WriteLine("Empty");
}

Use the appropriate helper instead:

if (string.IsNullOrEmpty(input))
{
    Console.WriteLine("Null or empty");
}

Ignoring whitespace-only input

A value such as " " is not empty:

Console.WriteLine("   ".Length); // 3

For user input, use:

if (string.IsNullOrWhiteSpace(input))
{
    Console.WriteLine("No meaningful text was entered.");
}

Assuming one spelling has a major performance advantage

Do not choose string.Empty or "" based on supposed allocation savings in ordinary code. Prefer clarity and your project's established style.

Using String inconsistently

String.Empty is correct, but mixing String and string throughout the same code can reduce consistency. Most C# style guides prefer the keyword form, string.

Comparisons

Expression or typeMeaningTypical use
""A non-null string with zero charactersMost direct empty-string assignment
string.EmptyNamed static empty string valueReadable alternative; team-style dependent
String.EmptyExactly the same value as string.EmptyValid, but string is commonly preferred in C# code
nullNo string value is presentOptional or unknown text
" "A string containing whitespace charactersInput that may need trimming or validation

string.IsNullOrEmpty(value) and string.IsNullOrWhiteSpace(value) also answer different questions:

MethodReturns true for nullReturns true for ""Returns true for " "
string.IsNullOrEmpty(value)YesYesNo
string.IsNullOrWhiteSpace(value)YesYesYes
Take Quiz

Cheat Sheet

// Common direct initialization
string value = "";

// Equivalent named form
string value2 = string.Empty;

// `string` and `String` name the same .NET type
String value3 = String.Empty;

// Empty string: exists and has zero characters
string empty = "";
int length = empty.Length; // 0

// Nullable string: may have no value
string? missing = null;

// Check null or empty
bool noText = string.IsNullOrEmpty(missing);

// Check null, empty, or whitespace only
bool noMeaningfulText = string.IsNullOrWhiteSpace("   ");
  • Prefer "" for a straightforward empty string assignment unless your project convention says otherwise.
  • Prefer lowercase C# aliases such as string in normal C# code.
  • Do not call members such as .Length on a value that may be null.
  • Use null and "" according to their business meaning; they are not interchangeable.

FAQ

Is string.Empty the same as "" in C#?

Yes. Both represent an empty, non-null string with zero characters.

Is String.Empty different from string.Empty?

No. string is a C# alias for System.String, so both access the same Empty field.

Which should I use: "" or string.Empty?

For direct assignments, "" is commonly preferred because it is concise. string.Empty is equally correct. Follow your team's style rules when they exist.

Does string.Empty perform better than ""?

There is no meaningful practical performance difference for normal code. Choose the clearer, consistent form.

Is an empty string the same as null?

No. "" is a real string of length zero. null means there is no string value. Their meanings and null-safety behavior differ.

How do I check whether a string is null or empty?

Use string.IsNullOrEmpty(value).

How do I treat spaces as empty too?

Use string.IsNullOrWhiteSpace(value), which also returns true for tabs, spaces, and other whitespace-only text.

Take Quiz

Mini Project

Description

Create a small profile-input normalizer. It accepts an optional display name, removes surrounding whitespace from valid names, and returns an empty string when no meaningful name was supplied. This mirrors common form and API input handling.

Goal

Write a method that always returns a non-null, cleaned display name.

Requirements

  • Create a method named NormalizeDisplayName that accepts string? input.
  • Return "" when the input is null, empty, or whitespace only.
  • Trim leading and trailing whitespace from a valid name.
  • Demonstrate the method with a null value, whitespace-only value, and valid value.
Take Quiz

Keep learning

Related questions

View more