How to Check if an Array is Empty in C#?

In this C# tutorial, I have explained, how to check if an array is empty in C#. Here I will show you, multiple ways to determine if a C# array is empty and provide examples for each method.

Check if an Array is Empty in C#

Here, I will show you the below 4 methods to check if an array is empty in C# with examples.

  • Using the Length Property
  • Using the Any Extension Method from LINQ
  • Using the Count Extension Method from LINQ
  • Using the Default Value of Arrays

1. Using the Length Property

The most straightforward way to check if an array is empty in C# is by using the Length property. All arrays in C# have this property, which returns the total number of elements in the array. An empty array will have a length of 0.

Example:

using System;

namespace ArrayEmptyCheck
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initializing an empty array
            int[] numbers = new int[0];

            // Check if the array is empty using the Length property
            if (numbers.Length == 0)
            {
                Console.WriteLine("The array is empty.");
            }
            else
            {
                Console.WriteLine("The array is not empty.");
            }

            // Keep the console open until a key is pressed
            Console.ReadKey();
        }
    }
}

Once you run the code, you can see the output in the following screenshot.

Check if an Array is Empty in C#

2. Using the Any Extension Method from LINQ

The Language Integrated Query (LINQ) in C# provides a handy Any() extension method which can be used to check if any elements exist in a collection. By default, without any parameters, Any() will return false if the collection is empty and true otherwise.

Example:

using System;
using System.Linq;

int[] numbers = new int[] { };
if (!numbers.Any())
{
    Console.WriteLine("The array is empty.");
}
else
{
    Console.WriteLine("The array is not empty.");
}

3. Using the Count Extension Method from LINQ

Similar to Any(), LINQ provides a Count() method which counts the number of elements in a collection. An empty array will return a count of 0.

Example:

using System;
using System.Linq;

int[] numbers = new int[] { };
if (numbers.Count() == 0)
{
    Console.WriteLine("The array is empty.");
}
else
{
    Console.WriteLine("The array is not empty.");
}

Note: While Any() and Count() are both useful, if you’re only interested in checking for emptiness, Any() is slightly more efficient. This is because Any() stops iterating once it finds an element, whereas Count() iterates over the entire collection.

4. Using the Default Value of Arrays

In C#, when an array is declared but not initialized, its default value is null. We can use this to check if an array is not only empty but also uninitialized.

Example:

using System;

namespace CheckArrayStatus
{
    class Program
    {
        static void Main(string[] args)
        {
            // Declare an array without initialization
            int[] numbers = null;

            // Check if the array is uninitialized or empty
            if (numbers == null || numbers.Length == 0)
            {
                Console.WriteLine("The array is either uninitialized or empty.");
            }
            else
            {
                Console.WriteLine("The array is not empty.");
            }

            // Keep the console open until a key is pressed
            Console.ReadKey();
        }
    }
}

When you run this program, it will display “The array is either uninitialized or empty.” due to the numbers array being set to null. If you initialize the numbers array with any elements or an empty set, the program will adjust its output accordingly.

You can look at the screenshot below:

How to Check if an Array is Empty in C#

Conclusion

In C#, checking whether an array is empty can be done in multiple ways. Depending on your specific requirements and the context of your code, you can choose the method that best fits your needs. Always keep performance considerations in mind, especially when working with large data sets. I hope you got an idea of how to check if an array is empty in C#.

You may also like: