How to check if an array is empty in C#?
To check an array is empty in C# you can use theLength
property which will return the number of elements in array, the array object may also by null in some cases and for that we can have the null check before checking the Length property to prevent any issues.Example of How to check if an array is empty in C#
using System;
public class Program
{
public static void Main()
{
int[] emptyArray = new int[] {};
if(emptyArray == null || emptyArray.Length == 0){
Console.WriteLine("array is empty");
}else{
Console.WriteLine("array is not empty");
}
}
}
Output
array is empty