Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IsNullOrEmpty equivalent for Array? C#

Tags:

arrays

c#

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 
like image 481
maxp Avatar asked Dec 19 '11 10:12

maxp


1 Answers

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.

like image 153
Polynomial Avatar answered Oct 03 '22 03:10

Polynomial