views:

231

answers:

3

An array is defined of assumed elements like I have array like String[] 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.

+5  A: 

Using LINQ you can try

int count = strArray.Count(x => x != null);
astander
You beat my 5.37 seconds :) But does the OP want null or non null elements?
slugster
True, sorry, fixed it up.
astander
@AstanderSir.. Thanks....
Harikrishna
+1  A: 

Use LINQ:

int i = (from s in strArray where !string.IsNullOrEmpty(s) select s).Count();
slugster
@slugster always rock!! :)
BreakHead
Mmmmm thanks?!?! The other guys had more concise answers than me.
slugster
@slugster Good Answer Thanks...
Harikrishna
+7  A: 

You can use Enumerable.Count:

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

Count iterates the enumerable array and counts the elements to which the specified predicate applies.

dtb
Here I can not get Count property of array like strArray here...
Harikrishna
The code uses LINQ. You need to add `using System.Linq;` at the top of your source file to make the LINQ extension methods visible.
dtb
Is it same to do like everytime checking for the each element of strArray that it is null or not in the for loop ?
Harikrishna
@Harikrishna: Yes.
dtb
@dtb Thanks....
Harikrishna