C# Enumerable Count(IEnumerable)

Description

Returns the number of elements in a sequence.

Syntax


public static int Count<TSource>(
  this IEnumerable<TSource> source
)

Parameters

  • TSource - The type of the elements of source.
  • source - A sequence that contains elements to be counted.

Returns

returns The number of elements in the input sequence.

Example

The following code example demonstrates how to use Count(IEnumerable) to count the elements in an array.


//from  w ww. java2s.  c om
using System;
using System.Linq;
using System.Collections.Generic;
public class MainClass{
  public static void Main(String[] argv){  
    string[] fruits = { "apple", "banana", "mango", "orange", "passionfruit", "grape" };
    int numberOfFruits = fruits.Count();
    Console.WriteLine(numberOfFruits);

  }
}
    

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Linq »




Enumerable