Array.Empty<T>()
to efficiently create empty arrays CS-P1009Using the new
keyword lets you create an array with the length of your choice. However, memory allocation, no matter how big or small, comes at a cost. Instead, it is recommended that you use Array.Empty<T>()
to create an empty array as it maintains a readonly
static
buffer internally, thereby essentially maintaining a single instance instead of multiple copies despite multiple invocations. Refer to the benchmarks mentioned in the references section below for additional info.
var zeroArray = new int[0] {};
var zeroArray = Array.Empty<int>();