Skip to main content
.NET Framework Class Library
EnumerableCountTSource Method (IEnumerableTSource)

Returns the number of elements in a sequence.

Namespace:   System.Linq
Assembly:  System.Core (in System.Core.dll)
<[%$TOPIC/bb338038_en-us_VS_110_1_0_0_0_0%]> _
Public Shared Function Count(Of TSource) ( _
	source As [%$TOPIC/bb338038_en-us_VS_110_1_0_0_0_1%](Of TSource) _
) As [%$TOPIC/bb338038_en-us_VS_110_1_0_0_0_2%]
public static [%$TOPIC/bb338038_en-us_VS_110_1_0_1_0_0%] Count<TSource>(
	this [%$TOPIC/bb338038_en-us_VS_110_1_0_1_0_1%]<TSource> source
generic<typename TSource>
static [%$TOPIC/bb338038_en-us_VS_110_1_0_2_0_1%] Count(
	[%$TOPIC/bb338038_en-us_VS_110_1_0_2_0_2%]<TSource>^ source
static member Count : 
        source:[%$TOPIC/bb338038_en-us_VS_110_1_0_3_0_0%]<'TSource> -> [%$TOPIC/bb338038_en-us_VS_110_1_0_3_0_1%]
JScript does not support generic types or methods.

Type Parameters


The type of the elements of source.


Type: System.Collections.GenericIEnumerableTSource

A sequence that contains elements to be counted.

Return Value

Type: SystemInt32
The number of elements in the input sequence.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerableTSource. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

source is .


The number of elements in source is larger than MaxValue.


If the type of source implements ICollectionT, that implementation is used to obtain the count of elements. Otherwise, this method determines the count.

Use the LongCount method when you expect and want to allow the result to be greater than MaxValue.

In Visual Basic query expression syntax, an Aggregate Into Count() clause translates to an invocation of Count.


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

        ' Create an array of strings. 
        Dim fruits() As String = {"apple", "banana", "mango", "orange", "passionfruit", "grape"}

            ' Count the number of items in the array. 
            Dim numberOfFruits As Integer = fruits.Count()
            ' Display the output.
            MsgBox("There are " & numberOfFruits & " fruits in the collection.")
        Catch e As OverflowException
            MsgBox("The count is too large to store as an Int32. Try using LongCount() instead.")
        End Try 

        ' This code produces the following output: 
        ' There are 6 fruits in the collection.
string[] fruits = { "apple", "banana", "mango", "orange", "passionfruit", "grape" };

                int numberOfFruits = fruits.Count();
                    "There are {0} fruits in the collection.",

            catch (OverflowException)
                Console.WriteLine("The count is too large to store as an Int32.");
                Console.WriteLine("Try using the LongCount() method instead.");

            // This code produces the following output: 
            // There are 6 fruits in the collection. 
Version Information

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8