How many elements of array are not null?
An array is defined of assumed elements like I have array like Stri开发者_Python百科ng[] strArray = new String[50];.
Now from 50 elements only some elements are assigned and remaining are left null then I want the number of assigned elements.
Like here only 30 elements are assigned then I want that figure.
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.
Using LINQ you can try
int count = strArray.Count(x => x != null);
Use LINQ:
int i = (from s in strArray where !string.IsNullOrEmpty(s) select s).Count();
精彩评论