IsNullOrEmpty equivalent for Array? C#

后端 未结 11 1434
轻奢々
轻奢々 2020-12-08 18:42

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

11条回答
  •  庸人自扰
    2020-12-08 19:16

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

    /// Indicates whether the specified array is null or has a length of zero.
    /// The array to test.
    /// true if the array parameter is null or has a length of zero; otherwise, false.
    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.

提交回复
热议问题