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.
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.nullmeans 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:
stringis C#'s built-in nickname.Stringis the .NET type name, short forSystem.String.
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;
isReady = ;
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:
messageis initialized to an empty, non-null string.message.Lengthis0, so the first line prints0.message += "Hello"creates and assigns a new string containingHello. Strings are immutable, so the original value is not modified in place.- The next line prints
Hello. string.IsNullOrEmpty(message)returnsfalsebecausemessagenow 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 "".
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:
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
Comparisons
| Expression or type | Meaning | Typical use |
|---|---|---|
"" | A non-null string with zero characters | Most direct empty-string assignment |
string.Empty | Named static empty string value | Readable alternative; team-style dependent |
String.Empty | Exactly the same value as string.Empty | Valid, but string is commonly preferred in C# code |
null | No string value is present | Optional or unknown text |
" " | A string containing whitespace characters | Input that may need trimming or validation |
and also answer different questions:
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
stringin normal C# code. - Do not call members such as
.Lengthon a value that may benull. - Use
nulland""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?
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
NormalizeDisplayNamethat acceptsstring?input. - Return
""when the input isnull, 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.
Keep learning
Related questions
AddTransient vs AddScoped vs AddSingleton in ASP.NET Core Dependency Injection
Learn the differences between AddTransient, AddScoped, and AddSingleton in ASP.NET Core DI with examples and practical usage.
Best Way to Repeat a Character in C#: Building Repeated Strings Efficiently
Learn the best way to repeat a character in C#, compare StringBuilder, string concatenation, and simpler built-in options.
C# Access Modifiers and static: public, private, protected, and Defaults
Learn how C# public, private, protected, and default access control visibility, and how static differs from instance members.