LINQ-Use of Count to get the number of items in the array.

This is a simple function to count number of items in Array using count Linq query.

 public void CountItems()
{
    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
 
    int Numbers = numbers.Count();
 
    Console.WriteLine("There are {0}  numbers in the list.", Numbers);
}


to count odd numbers in Array use following code:

public void CountOddNumber()
{
            int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

            int oddNumbers = numbers.Count(n => n % 2 == 1);

            Console.WriteLine("There are {0} odd numbers in the list.", oddNumbers);
}


Ebook Download
View all
Learn
View all