IsNullOrEmpty equivalent for Array? C#

后端 未结 11 1444
轻奢々
轻奢々 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:34

    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 (this ICollection collection)
     {
        if (collection == null || collection.Count == 0)
            return true;
        else
           return collection.All(item => item == null);
     }
    

提交回复
热议问题