Empty strings. The C# string.Empty field is an empty string literal. It is not the same as an empty string literal constant—there is a subtle difference.
Notes, empty strings. We compare ways to create, and test, empty strings. An understanding of string literals is helpful here. There are some minor performance differences.
Part 3 Here we use a switch statement on string.Empty. We cannot use string.Empty as a case, as it is not a constant.
using System;
class Program
{
static void Main()
{
// Part 1: initialize string to empty string field.
string value = string.Empty;
if (value == "")
{
Console.WriteLine("Empty");
}
// Part 2: test against Length.
if (value.Length == 0)
{
Console.WriteLine("Empty");
}
// Part 3: switch on the string constant.// ... We cannot have string.Empty as a case.switch (value)
{
case "":
{
Console.WriteLine("Empty");
break;
}
}
}
}Empty
Empty
Empty
Has characters. An important point is to focus on the intention of code. If we want to see if a string has characters, IsNullOrEmpty is a good choice.
Tip Suppose we have a slow part of our code, and we want to avoid running if unless a string has characters.
Info With IsNullOrEmpty, code is easier to understand and less likely to have mistakes in the logic that cause the slow path to be run.
using System;
class Program
{
static void Main()
{
string animal = "";
if (!string.IsNullOrEmpty(animal))
{
// Do something that might be slow.
Console.WriteLine(animal.ToUpper());
}
else
{
Console.WriteLine("SKIPPED");
}
}
}SKIPPED
Benchmark, empty string. An empty string has 0 chars. These strings can be checked using a clear and efficient method. Using the fastest method can help many programs.
Version 1 We test an empty string against the empty string literal. If both strings are literals, this would be faster.
Result For testing strings that are not constant string literals (and are not null), using Length is the fastest option.
using System;
using System.Diagnostics;
class Program
{
static void Main()
{
// Get empty string.
string input = "a".Replace("a", "");
int count = 0;
const int m = 100000000;
// Version 1: test against empty literal.
Stopwatch s1 = Stopwatch.StartNew();
for (int i = 0; i < m; i++)
{
if (input == "")
{
count++;
}
}
s1.Stop();
// Version 2: use IsNullOrEmpty.
Stopwatch s2 = Stopwatch.StartNew();
for (int i = 0; i < m; i++)
{
if (string.IsNullOrEmpty(input))
{
count++;
}
}
s2.Stop();
// Version 3: test Length.
Stopwatch s3 = Stopwatch.StartNew();
for (int i = 0; i < m; i++)
{
if (input.Length == 0)
{
count++;
}
}
s3.Stop();
Console.WriteLine(s1.ElapsedMilliseconds);
Console.WriteLine(s2.ElapsedMilliseconds);
Console.WriteLine(s3.ElapsedMilliseconds);
}
}191 ms == ""
71 ms IsNullOrEmpty
26 ms Length == 0
Benchmark, string.Empty. When you specify "" instead string.Empty, the C# compiler itself will know the value of the string data. The runtime is not a part of this decision.
Info You can run a benchmark where string.Empty is much slower than "", by using a conditional that is removed by the C# compiler.
Version 1 This code tests the string.Empty read only field against the null literal in a tight loop.
Version 2 Here we test the empty string literal against null in a tight loop. The constant comparison can be removed at compile-time.
Result Certain C# compiler optimizations are effective only with "", not string.Empty.
using System;
using System.Diagnostics;
class Program
{
const int _max = 100000000;
static void Main()
{
var s1 = Stopwatch.StartNew();
// Version 1: test string.Empty.for (int i = 0; i < _max; i++)
{
if (string.Empty == null)
{
throw new Exception();
}
}
s1.Stop();
var s2 = Stopwatch.StartNew();
// Version 2: test string literal.for (int i = 0; i < _max; i++)
{
if ("" == null)
{
throw new Exception();
}
}
s2.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns"));
}
}0.57 ns string.Empty
0.28 ns ""
Implementation. Here we look inside .NET and see how the Empty field is initialized. In the String class, look at the static constructor.
And The Empty field is assigned to the constant "". This constant is a string literal instance.
Switch. Because string.Empty is initialized in the static constructor for the String class, it cannot be determined when you compile your program. It is a readonly field.
Notes, string switch. Internally, complex switch statements are implemented as a Dictionary, providing constant lookup times. This is sometimes a speedup.
Notes, equality operator. If you look at == in the IL, it is called op_Equality. This operator compiles to a method call (like any other). The == is just syntactic sugar.
A summary. An empty string can be checked in many ways. We choose which one depending on clarity and convention (what is used already). The string.Empty field can be used.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 21, 2021 (image).