In this article, we are going to look at the numerous ways to declare an empty array in C#.

Arrays are used to store multiple variables of the same type. Sometimes our solution design may require the use of an empty array. For example, as an initial value for a dynamic-sized collection, or in cases where a method would usually return a list of results, an empty array could indicate no results. We shall use string arrays for our examples but the concept could be applied to the other datatypes as well.

To download the source code for this article, you can visit our GitHub repository.

Let’s start.

Support Code Maze on Patreon to get rid of ads and get the best discounts on our products!
Become a patron at Patreon!

Use the “new” Keyword

The natural way of declaring an object would be to use the new keyword and C# provides us with some ways to do it.

The Array Initializer Syntax

We will start with one of the classic ways to initialize an array:

var myArray = new string[0];

A drawback of this method is that it allocates a new array of length 0.  This could result in unnecessary memory usage.

Use an Empty String Literal

Next, we’ll see how to initialize the array using an empty string literal:

var myArray = new string[] { };

Or:

string[] myArray = { };

This yields the same result as the array initializer syntax but has the same drawback. It’s also slightly less readable.

Use the Enumerable Class

The Enumerable class from the System.Linq namespace gives us two ways of doing it. Let’s look at each of them.

Use the Empty Method

In this approach, we use LINQ to create an empty IEnumerable<string> sequence and then convert it to an array using the ToArray() method:

var myArray = Enumerable.Empty<string>().ToArray();

Use the Repeat Method

We can also use the Enumerable.Repeat() method to repeat the empty string 0 times and then convert it to an array using the ToArray() method:

var myArray = Enumerable.Repeat("", 0).ToArray();

Or:

var myArray = Enumerable.Repeat(string.Empty, 0).ToArray();

The methods using the Enumerable class are less performant because they involve the creation of an empty IEnumerable<string> sequence which may be unnecessary.

Use the Array Class

Next, we will look at the Empty method from System.Array to declare the empty string array:

var myArray = Array.Empty<string>();

This method is concise and performs well. It creates an empty array with zero elements, without the overhead of allocating memory for a new array with a length of 0. In addition, it is also very readable and conveys the intention of creating an empty array.

Conclusion

We have explored multiple ways to declare an empty string array in C# and some of the pros and cons for each of them. Ultimately though, the performance differences are negligible and it comes down to the preference of the developer to implement the method that suits them best.

Liked it? Take a second to support Code Maze on Patreon and get the ad free reading experience!
Become a patron at Patreon!