IsNullOrEmpty equivalent for Array? C#

C#Arrays

C# Problem Overview


Is there a function in the .NET library that will return true or false as to whether an array is null or empty? (Similar to string.IsNullOrEmpty).

I had a look in the Array class for a function such as this but couldn't see anything.

i.e.

var a = new string[]{};
string[] b = null;
var c = new string[]{"hello"};

IsNullOrEmpty(a); //returns true
IsNullOrEmpty(b); //returns true
IsNullOrEmpty(c); //returns false

C# Solutions


Solution 1 - C#

There isn't an existing one, but you could use this extension method:

/// <summary>Indicates whether the specified array is null or has a length of zero.</summary>
/// <param name="array">The array to test.</param>
/// <returns>true if the array parameter is null or has a length of zero; otherwise, false.</returns>
public static bool IsNullOrEmpty(this Array array)
{
    return (array == null || array.Length == 0);
}

Just place this in an extensions class somewhere and it'll extend Array to have an IsNullOrEmpty method.

Solution 2 - C#

You could create your own extension method:

public static bool IsNullOrEmpty<T>(this T[] array)
{
    return array == null || array.Length == 0;
}

Solution 3 - C#

With Null-conditional Operator introduced in VS 2015, the opposite IsNotNullOrEmpty can be:

if (array?.Length > 0) {           // similar to if (array != null && array.Length > 0) {

but the IsNullOrEmpty version looks a bit ugly because of the operator precedence:

if (!(array?.Length > 0)) {

Solution 4 - C#

More generic if you use ICollection<T>:

public static bool IsNullOrEmpty<T> (this ICollection<T> collection)
{
	return collection == null || collection.Count == 0;
}

Solution 5 - C#

This is an updated C# 8 version of the (currently) up-voted answer

public static bool IsNullOrEmpty<T>([NotNullWhen(false)] this T[]? array) =>
    array == null || array.Length == 0;

Solution 6 - C#

In case you initialized you array like

string[] myEmpytArray = new string[4];

Then to check if your array elements are empty use

myEmpytArray .All(item => item == null)

Try

 public static bool IsNullOrEmpty<T> (this ICollection<T> collection)
 {
    if (collection == null || collection.Count == 0)
        return true;
    else
       return collection.All(item => item == null);
 }

Solution 7 - C#

You can also use Any on creating your extension method:

public static bool IsNullOrEmpty<T>(this T[] array) where T : class
    {
        return (array == null || !array.Any());
    }

Don't forget to add using System.Linq; on your using statements.

Solution 8 - C#

if (array?.Any() != true) { ... }
  • Don't forget having using System.Linq;
  • Note one must explicitly compare against true since the underlying type is bool?

Solution 9 - C#

No, but you can write it yourself as an extension method. Or a static method in your own library, if you don't like calling methods on a null type.

Solution 10 - C#

Checking for null or empty or Length has been simplified with C# Linq. With null coalesce operator, one can simply do this:

if (array?.Any())
    return true;
else return false;

Solution 11 - C#

Since C# 6.0, the null-propagation operator may be used to express concise like this:

if (array?.Count.Equals(0) ?? true)

> Note 1: ?? false is necessary, because of the following reason > > Note 2: as a bonus, the statement is also "thread-safe"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmaxpView Question on Stackoverflow
Solution 1 - C#PolynomialView Answer on Stackoverflow
Solution 2 - C#LeeView Answer on Stackoverflow
Solution 3 - C#SlaiView Answer on Stackoverflow
Solution 4 - C#Danyal AytekinView Answer on Stackoverflow
Solution 5 - C#pulviniusView Answer on Stackoverflow
Solution 6 - C#jabu.hlongView Answer on Stackoverflow
Solution 7 - C#Willy David JrView Answer on Stackoverflow
Solution 8 - C#Shmil The CatView Answer on Stackoverflow
Solution 9 - C#Yuriy FaktorovichView Answer on Stackoverflow
Solution 10 - C#GauravsaView Answer on Stackoverflow
Solution 11 - C#Teodor TiteView Answer on Stackoverflow