Enumerable.Empty Vs Array Initialization Syntax

The following set of code showcases two most commonly used methods to generate an empty array in C#.

var array1 = Enumerable.Empty<string>();
var array3 = new string[0];

What would be the output of the above lines of code ? One might be tempted to say both returns True. However, that isn’t quite right. The output is as follows.

Enumerable Based => True
Array Based => False

The Enumerable.Empty returns the same empty array each time you request it, while the array initialization based approach, as noticed from above code, references difference array instances. That is a subtle, but significant difference. This is evident when you lookup the source code of Enumerable.Empty method in referencesource.microsoft.com

internal class EmptyEnumerable<TElement>
{
public static readonly TElement[] Instance = new TElement[0];
}

public static IEnumerable<TResult> Empty<TResult>() 
{
return EmptyEnumerable<TResult>.Instance;
}

As you can see, the Enumerable.empty uses the array approaches internally, but it is a static instance. This kind of explains the behavior.

Let’s examine further by benchmarking the performance using Benchmark.Net. For the tests, let’s create following class.

[MemoryDiagnoser]
public class BenchMarkTest
{
        [Benchmark]
        public void MethodWithEnumerable()
        {
            var array1 = Enumerable.Empty<string>();
            var array2 = Enumerable.Empty<string>();
            var output = ReferenceEquals(array1, array2);
        }

        [Benchmark]
        public void MethodWithArray()
        {
            var array1 = new string[0];
            var array2 = new string[0];
            var output = ReferenceEquals(array1, array2);
        }
}

The output is as follows

 Method  Mean Error StdDev Gen 0 Allocated
MethodWithEnumerable    11.630 ns  0.2632 ns  0.2703 ns  –   0 B
 MethodWithArray  7.258 ns  0.0905 ns  0.0756 ns   0.0152  24 B

As you can see, the though the array approach is slightly faster, it also consumes a bit more memory (understandably). Hence, it would depend on the context of the application you are developing that would decide which approach to use.

Advertisement

2 thoughts on “Enumerable.Empty Vs Array Initialization Syntax

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s