C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
And: This means string.Empty cannot be used in a switch. But we can test with string.Empty in if-statements.
IfPart 1: We assign to string.Empty. This is mostly equivalent to "". We test the variable against the constant "", which returns true.
Part 2: The program tests the Length of the Empty string. Its length is 0, so the string Empty is printed.
ConsolePart 3: Here we use a switch statement on string.Empty. We cannot use string.Empty as a case, as it is not a constant.
C# program that uses string.Empty
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;
}
}
}
}
Output
Empty
Empty
Empty
Version 1: We test an empty string against the empty string literal. If both strings are literals, this would be faster.
string.InternVersion 2: This code uses string.IsNullOrEmpty, and it has slightly different logic (it handles null).
Version 3: This version uses the Length property. The Length check throws a NullReferenceException when the string is null.
Null StringsNullReferenceExceptionResult: For testing strings that are not constant string literals (and are not null), using Length is the fastest option.
C# program that benchmarks empty strings
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);
}
}
Output
191 ms == ""
71 ms IsNullOrEmpty
26 ms Length == 0
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.
C# program that benchmarks empty strings
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"));
}
}
Output
0.57 ns string.Empty
0.28 ns ""
And: The Empty field is assigned to the constant "". This constant is a string literal instance.
Internal implementation of string.Empty:
static String()
{
Empty = "";
//
// More initialization omitted
//
}
Field declaration for string.Empty:
public static readonly string Empty;
Important: The case statements in switches can only contain const strings. So string.Empty, a readonly field, cannot be used.
const