C#

C#

Made by DeepSource

Use Array.Empty<T>() to efficiently create empty arrays CS-P1009

Performance
Major
Autofix

Using 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.

Bad Practice

var zeroArray = new int[0] {};

Recommended

var zeroArray = Array.Empty<int>();

Reference