c sharp

How to Declare an Empty Array in C#

Declaring an empty array in C# is a crucial programming task that can be helpful for future data storage, placeholders or default values, and computational purposes. There are multiple ways to declare an empty array in C# and this article will discuss each of them in detail.

Declare an Empty Array in C#

The empty array declaration in C# can be done:

1: Declare an Empty Array Using Simple Array Format

In C#, the empty array can be declared easily using a simple array format. One way to do this is by initializing an array with a size of 0. The following syntax will be used for empty array declaration in C#.

var myArray = new int[0];

In the above syntax, myArray is a variable defined for the empty array of integers. By setting the value to 0 in the square brackets, you can create an empty array.

The following example code snippet uses the simple array format for array declaration in C#.

using System;

class Example
{
    static void Main()
    {
        // Empty array Declaration
        int[] myArray = new int[0];
        // Checking Array Length
        Console.WriteLine("Array Length: " + myArray.Length);  
        // Resizing the array to add elements
        Array.Resize(ref myArray, 3);
        myArray[0] = 10;
        myArray[1] = 20;
        myArray[2] = 30;
        // Checking the length and elements of resized array
        Console.WriteLine("Array Length: " + myArray.Length);  
        Console.WriteLine("Array Elements: ");
        foreach (int element in myArray)
        {
            Console.WriteLine(element);
        }
        Console.ReadLine();
    }
}

The given code declares an empty integer array and checks its length. It then resizes the array to fit the three elements and assigns them the values. Finally, it shows the length and elements of the resized array.

2: Declare an Empty Array Using Empty String Literal

The empty string literal can also be used in C# for array declaration and the syntax to use it is given below:

int[] myArray = { };

The following example uses the empty string literal for array declaration in C#.

using System;

class Example
{
    static void Main()
    {
        // Empty array declaration using string literals
        int[] myArray = { };
        // Checking array length
        Console.WriteLine("Array Length: " + myArray.Length);
        // Resizing the array to add elements
        Array.Resize(ref myArray, 3);
        myArray[0] = 10;
        myArray[1] = 20;
        myArray[2] = 30;
        // Checking the length and elements of the resized array
        Console.WriteLine("Array Length: " + myArray.Length);
        Console.WriteLine("Array Elements: ");
        foreach (int element in myArray)
        {
            Console.WriteLine(element);
        }
        // Wait for user input to close the console window
        Console.ReadLine();
    }
}

The above code is similar to the one previously used in the first method; however, it uses the empty string literal instead to declare an empty array.

3: Declare an Empty Array Using Enumerable Class

The Enumerable class in C# is used under System.Linq namespace and provides the users two different ways to initialize the empty string. One is using the Enumerable.Empty() method that uses the following syntax:

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

This method uses the Enumerable.Empty() method to create an empty enumerable and then calls the ToArray() method to convert the enumeration to an array. Therefore, myArray will be an empty string.

 The following example uses the Enumerable.Empty() method for array declaration in C#.

using System;
using System.Linq;

class Example
{
    static void Main()
    {
        // Empty array declaration using Enumerable.Empty()
        string[] myArray = Enumerable.Empty().ToArray();
        // Checking array length
        Console.WriteLine("Array Length: " + myArray.Length);
        // Resizing the array to add elements
        Array.Resize(ref myArray, 3);
        myArray[0] = "10";
        myArray[1] = "20";
        myArray[2] = "30";
        // Checking the length and elements of the resized array
        Console.WriteLine("Array Length: " + myArray.Length);
        Console.WriteLine("Array Elements: ");
        foreach (string element in myArray)
        {
            Console.WriteLine(element);
        }
        // Wait for user input to close the console window
        Console.ReadLine();
    }
}

The other method from the enumerable class is using the Enumerable.Repeat method whose syntax is given below:

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

This method uses the Enumerable.Repeat() method, where an empty string “” is repeated 0 times, resulting in an empty string array stored in myArray.

The following example uses the Enumerable.Repeat() method for array declaration in C#.

using System;
using System.Linq;

class Example
{
    static void Main()
    {
        // Empty array declaration using Enumerable.Repeat
        string[] myArray = Enumerable.Repeat("", 0).ToArray();
        // Checking array length
        Console.WriteLine("Array Length: " + myArray.Length);
        // Resizing the array to add elements
        Array.Resize(ref myArray, 3);
        myArray[0] = "10";
        myArray[1] = "20";
        myArray[2] = "30";
        // Checking the length and elements of the resized array
        Console.WriteLine("Array Length: " + myArray.Length);
        Console.WriteLine("Array Elements: ");
        foreach (string element in myArray)
        {
            Console.WriteLine(element);
        }
        // Wait for user input to close the console window
        Console.ReadLine();
    }
}

4: Declare an Empty Array Using Array Class

The final method used for empty array declaration is the use of array class, which is considered to be a better approach since it does not overhead the memory and creates an empty array with zero elements.

The syntax to declare an empty array using the array class in C# is given below:

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

Note: Declaring an empty array means it has a length or size of 0. 

The below-given code uses the array class method for empty array declaration in C#.

using System;

class Example
{
    static void Main()
    {
        // Empty array declaration using Array.Empty
        var myArray = Array.Empty();
        // Checking array length
        Console.WriteLine("Array Length: " + myArray.Length);
        // Resizing the array to add elements
        Array.Resize(ref myArray, 3);
        myArray[0] = "10";
        myArray[1] = "20";
        myArray[2] = "30";
        // Checking the length and elements of the resized array
        Console.WriteLine("Array Length: " + myArray.Length);
        Console.WriteLine("Array Elements: ");
        foreach (string element in myArray)
        {
            Console.WriteLine(element);
        }
        // Wait for user input to close the console window
        Console.ReadLine();
    }
}

Bottom Line

An empty array in C# can be declared using several methods, such as Simple Array Format, Empty String Literal, Enumerable Class, and Array Class. Understanding these different methods will allow you to choose the most suitable approach for declaring an empty array based on your specific requirements and coding style.

About the author

Zainab Rehman

I'm an author by profession. My interest in the internet world motivates me to write for Linux Hint and I'm here to share my knowledge with others.