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.
Update for 2025: These days you can use the null conditional operator and null coalescing operator to simplify the check inline, e.g. (array?.Length ?? 0) == 0 - the array?.Length part evaluates to the length of the array if it is not null, or null otherwise. The (...) ?? 0 part evaluates to the left hand expression if its value is not null, otherwise zero. By combining the two, you get the length of the array if it is not null, or zero if the array is null. The brackets are required due to operator precedence (?? has lower precedence than ==).
Naturally, you could still wrap this in an 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?.Length ?? 0) == 0;
}
For correct behaviour with very large arrays, one should use LongLength instead of Length.
Since it came up in the comments, it is important to note that calling an extension method on a null object is 100% valid and will not throw a NullReferenceException in the same way that calling an instance method on a null object would. This is because extension methods are implemented as static methods, so when you type array.IsNullOrEmpty() what you're really getting under the hood is MyExtensionMethods.IsNullOrEmpty(array), which operates in exactly the same way as the String.IsNullOrEmpty() static method.
if (arr != null && arr.Length != 0)? Or create an extension method if you need to use this repeatedly.if (arr != null && arr.Length != 0)in its entirety.IsNullOrEmptymethod—it's just there for convenience reasons. Early versions of the JITer actually had problems getting the optimization right.