Enumerable.TryGetNonEnumeratedCount<TSource> Method

Definition

Attempts to determine the number of elements in a sequence without forcing an enumeration.

public static bool TryGetNonEnumeratedCount<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, out int count);

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IEnumerable<TSource>

A sequence that contains elements to be counted.

count
Int32

When this method returns, contains the number of elements in source, or 0 if the count couldn't be determined without enumeration.

Returns

true if the count of source can be determined without enumeration; otherwise, false.

Remarks

The method performs a series of type tests, identifying common subtypes whose count can be determined without enumerating. This includes ICollection<T>, ICollection, and internal types used in the LINQ implementation.

The method is typically a constant-time operation, but ultimately this depends on the complexity characteristics of the underlying collection's implementation.

Applies to

Produk Versi
.NET 6, 7, 8, 9