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.
var array3 = Array.Empty()
LikeLike
Hi
Thank you for bringing Array.Empty up. If you look up the source code for Array.Empty, it is a lot similar to Enumerable.Empty. Internally both uses “new T[0]” for generating the empty array.
https://referencesource.microsoft.com/#mscorlib/system/array.cs,bc9fd1be0e4f4e70,
LikeLike