How many elements of array are not null?

前端 未结 3 1182
粉色の甜心
粉色の甜心 2020-12-31 01:57

An array is defined of assumed elements like I have array like String[] strArray = new String[50];.

Now from 50 elements only some elements

相关标签:
3条回答
  • 2020-12-31 02:33

    Use LINQ:

    int i = (from s in strArray where !string.IsNullOrEmpty(s) select s).Count();
    
    0 讨论(0)
  • 2020-12-31 02:39

    You can use Enumerable.Count:

    string[] strArray = new string[50];
    ...
    int result = strArray.Count(s => s != null);
    

    This extension method iterates the array and counts the number of elements the specified predicate applies to.

    0 讨论(0)
  • 2020-12-31 02:47

    Using LINQ you can try

    int count = strArray.Count(x => x != null);
    
    0 讨论(0)
提交回复
热议问题