How to check if an array is empty in C#
In this tutorial, we are going to learn about how to check if an array is empty or not in C#.
Checking the empty array
To check if an given array is empty or not, we can use the built-in Array.Length property in C#.
Here is an example:
using System;
class Check {
static void Main() {
int[] myArr = new int[] {};
if (myArr.Length == 0) {
Console.WriteLine("array is empty");
}else{
Console.WriteLine("array is not empty");
}
}
}Alternatively, we can also use the Array.Length property to check if a array is null or empty in C#.
using System;
class Check {
static void Main() {
int[] myArr = new int[] {};
if (myArr == null || myArr.Length == 0) {
Console.WriteLine("array is empty");
}else{
Console.WriteLine("array is not empty");
}
}
}Note: The string.Length property returns the total number of elements in a given Array.


