Skip to main content

How to Use string.Empty for an Empty String in C#

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

string.Empty is the empty string: the same value as "", with a length of 0. Start a string property with = string.Empty; and it's never null, so your code can call its methods straight away.

Microsoft's reference: Represents the empty string. This field is read-only. And: The value of this field is the zero-length string, "". It's declared as public static readonly string Empty;: a static field of the String class, so you read it through the type, as string.Empty.

Its most common job is a starting value. The reference again: In application code, this field is most commonly used in assignments to initialize a string variable to an empty string. An empty string is a real string, so you can call its methods. Microsoft's guide to strings: By initializing strings with the Empty value instead of null, you can reduce the chances of a NullReferenceException occurring.

Syntax​

Syntax
string <name> = string.Empty;
public string <Property> { get; set; } = string.Empty;
<name> = string.Empty;
PartWhat it is
string.EmptyThe empty string, a string with no characters. String.Empty is the same field, because string is C#'s name for String
string <name> = string.Empty;A variable that starts as the empty string instead of null
public string <Property> { get; set; } = string.Empty;A property that starts as the empty string in every new object
<name> = string.Empty;Clears a string you already have

The Example​

A console app called StringEmpty first checks what string.Empty is. Then it makes a Contact whose Name and Phone properties start as string.Empty, uses them before anything is set, sets the name and clears it again. Square brackets around each value show where an empty string starts and ends.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n StringEmpty
cd StringEmpty
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/StringEmpty/StringEmpty.csproj:
Determining projects to restore...
Restored /Users/you/source/StringEmpty/StringEmpty.csproj (in 43 ms).
Restore succeeded.

dotnet new console -n StringEmpty creates a folder named StringEmpty with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or StringEmpty.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First compare string.Empty with "":

Program.cs (what string.Empty is)
// string.Empty is the empty string: the same value as "", with no characters in it.
Console.WriteLine($"string.Empty == \"\": {string.Empty == ""}");
Console.WriteLine($"string.Empty.Length: {string.Empty.Length}");

string.Empty == "" compares the two values, and Length counts the characters. The \" inside the interpolated string prints a double quote.

Then a new contact, used before anything is set:

Program.cs (a new contact)
// A new contact: Name and Phone start as string.Empty, so they are never null.
Contact contact = new();
Console.WriteLine($"New contact: name [{contact.Name}], phone [{contact.Phone}]");

// You can call string methods on them straight away, without checking for null.
Console.WriteLine($"Name in capitals: [{contact.Name.ToUpper()}], phone length: {contact.Phone.Length}");

contact.Name.ToUpper() and contact.Phone.Length call members of the two strings without checking for null first.

Then set the name, and clear it:

Program.cs (set and clear)
// Fill in a value later, like any other string.
contact.Name = "Maya";
Console.WriteLine($"After setting the name: [{contact.Name.ToUpper()}]");

// Assign string.Empty to clear it again.
contact.Name = string.Empty;
Console.WriteLine($"After clearing the name: [{contact.Name}], length {contact.Name.Length}");

A property that starts as string.Empty is an ordinary string: you set it like any other, and assigning string.Empty empties it again.

Last, the class:

Program.cs (the Contact class)
class Contact
{
public string Name { get; set; } = string.Empty;
public string Phone { get; set; } = string.Empty;
}

= string.Empty; after each property's { get; set; } is its starting value, so every new Contact gets an empty Name and Phone.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
string.Empty == "": True
string.Empty.Length: 0
New contact: name [], phone []
Name in capitals: [], phone length: 0
After setting the name: [MAYA]
After clearing the name: [], length 0

macOS-style terminal window titled Terminal — StringEmpty showing dotnet run printing string.Empty == "": True, string.Empty.Length: 0, New contact: name and phone as empty square brackets, Name in capitals: empty brackets with phone length: 0, After setting the name: MAYA, and After clearing the name: empty brackets with length 0

string.Empty == "" printed True, and its length was 0: it's the same value as "". The new contact's name and phone printed as [], with nothing between the brackets, and ToUpper() and Length worked on them although no value had been set. After Name = "Maya", ToUpper() gave MAYA, and after Name = string.Empty the name was empty again, with a length of 0.

The Complete File​

macOS-style code window titled Program.cs — StringEmpty showing string.Empty compared with an empty string literal and its Length printed, a new Contact whose empty Name and Phone are printed in square brackets, ToUpper and Length called on them, the name set to Maya and cleared again with string.Empty, and the Contact class whose Name and Phone properties start as string.Empty

Program.cs
// string.Empty is the empty string: the same value as "", with no characters in it.
Console.WriteLine($"string.Empty == \"\": {string.Empty == ""}");
Console.WriteLine($"string.Empty.Length: {string.Empty.Length}");

// A new contact: Name and Phone start as string.Empty, so they are never null.
Contact contact = new();
Console.WriteLine($"New contact: name [{contact.Name}], phone [{contact.Phone}]");

// You can call string methods on them straight away, without checking for null.
Console.WriteLine($"Name in capitals: [{contact.Name.ToUpper()}], phone length: {contact.Phone.Length}");

// Fill in a value later, like any other string.
contact.Name = "Maya";
Console.WriteLine($"After setting the name: [{contact.Name.ToUpper()}]");

// Assign string.Empty to clear it again.
contact.Name = string.Empty;
Console.WriteLine($"After clearing the name: [{contact.Name}], length {contact.Name.Length}");

class Contact
{
public string Name { get; set; } = string.Empty;
public string Phone { get; set; } = string.Empty;
}

Common Mistakes​

MistakeWhat happensDo this instead
Using string.Empty as a default parameter valuestring suffix = string.Empty in a method's parameter list didn't build: error CS1736: Default parameter value for 'suffix' must be a compile-time constantWrite string suffix = "". Label("Maya") then printed [Maya], and Label("Maya", " (work)") printed [Maya (work)]
Using string.Empty as a case labelcase string.Empty: didn't build: error CS9135: A constant value of type 'string' is expectedWrite case "":, which printed No phone number for an empty phone
Comparing with == string.Empty when the string can be nullFor string? phone = null, phone == string.Empty printed False, so the check missed the missing phoneUse string.IsNullOrEmpty(phone), which printed True. The reference: To test whether the value of a string is either null or String.Empty, use the IsNullOrEmpty method.

FAQ​

Should I write string.Empty or ""?​

They're the same value, as the run showed, so either works wherever a value is all you need. Where C# needs a compile-time constant, only "" works. Besides the default parameter and the case label above, const string NoPhone = string.Empty; didn't build (error CS0133: The expression being assigned to 'NoPhone' must be constant), and neither did [Obsolete(string.Empty)] (error CS0182: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type).

The reason is the readonly in its declaration. The const reference explains the difference: although a const field is a compile-time constant, the readonly field can be used for run-time constants.

Is an empty string the same as null?​

No. An empty string is a string with no characters, and null is no string at all. That's why phone == string.Empty was False for a null phone, and why ToUpper() worked on the new contact's empty Name. The strings guide: You can call methods on empty strings because they're valid System.String objects.

Conclusion​

string.Empty is the empty string, equal to "". Use it as the starting value of string variables and properties so they're never null, and to clear a string. Where C# needs a compile-time constant, such as a default parameter value, a case label, a const or an attribute argument, write "" instead. To test for "null or empty", use string.IsNullOrEmpty.

Additional Resources​