C# Array - Empty() Method



The C# Array Empty() method returns a singleton instance of an empty array without creating a new one each time.

Singleton is a creational design pattern that makes sure that only one object of its kind is available and provides a single point of access to it for any other code.

This method does not allocate memory for an empty array.

Syntax

Following is the syntax of the C# Array Empty() method −

public static T[] Empty<T> ();

Parameters

This method does not accepts any parameters −

Return value

This method returns an empty array of type T.

Example 1: Create an empty array of Integer

This is the basic example of the Empty() method, that shows how to create an empty array of integer −

using System;
class Program
{
   static void Main()
   {
      // Create an empty array of integers
      int[] emptyArray = Array.Empty<int>();

      Console.WriteLine("Array length: " + emptyArray.Length);
   }
}

Output

Following is the output −

Array length: 0

Example 2: Default value for optional parameter

In this example, Using the Empty() method for optional parameters in methods ensure you do not create a new array unnecessarily. −

using System;
class Program {
   public void PrintNumbers(int[] numbers = null) {
      if (numbers == null) {
         numbers = Array.Empty<int>();
      }

      if (numbers.Length == 0) {
         Console.WriteLine("No numbers to display.");
      } else {
         Console.WriteLine("Numbers: " + string.Join(", ", numbers));
      }
   }
   static void Main() {
      Program program = new Program();
      program.PrintNumbers();
      program.PrintNumbers(new int[] { 1, 2, 3 });
   }
}

Output

Following is the output −

No numbers to display.
Numbers: 1, 2, 3

Example 3: Array.Empty<T>() with Generic Collections

This example demonstrates how to use Empty() method in a complex scenario where it might be used with generics or a collection of a specific type −

using System;
class Program
{
   static void ProcessArray<T>(T[] array)
   {
      Console.WriteLine("Processing array of type: " + typeof(T).Name);
      Console.WriteLine("Array length: " + array.Length);
   }

   static void Main()
   {
      double[] emptyDoubleArray = new double[0];

      ProcessArray(emptyDoubleArray);
   }
}

Output

Following is the output −

Processing array of type: Double
Array length: 0

Example 4: Create an Empty Array of Strings

Following is another example of the Empty() method. Here, we create an empty array of string −

using System;

class Program
{
   static void Main()
   {
      string[] array = new string [] {"aman", "kumar", "gupta", "tutorialspoint"};
      
      // create an empty string Array
      string[] emptyArray = Array.Empty<string>();
      
      Console.WriteLine("Array length: " + emptyArray.Length);
      
      // display array element
      for(int i =0; i<array.Length; i++){
        Console.Write(array[i] + " ");
      }
   }
}

Output

Following is the output −

Array length: 0
aman kumar gupta tutorialspoint
csharp_array_class.htm
Advertisements