CKTIS / C#
Enumerable.Count: practical reference
Enumerable.Count: practical reference: Returns the number of all elements or of elements satisfying a predicate. It may read a collection Count without enumeration, but enumerates a general IEnumerable<T>.
Version: .NET LINQ; verified on .NET 10.0.14
Syntax
source.Count(/* overload-specific arguments */)Returns the number of all elements or of elements satisfying a predicate. It may read a collection Count without enumeration, but enumerates a general IEnumerable.
Use it when an exact count within Int32 is required. Use Any to test whether an element exists.
The predicate runs for every element and can have side effects. LongCount is appropriate when the result can exceed Int32.MaxValue.
Exceeding Int32.MaxValue throws OverflowException. An empty source gives 0; a null source or predicate gives ArgumentNullException.
The complete program actually uses Enumerable.Count; its exact expected stdout is 2.
Examples
using System;
using System.Collections.Generic;
using System.Linq;
Console.WriteLine(new[] { 1, 2, 3, 4 }.Count(value => value % 2 == 0));
Result
2
The complete program actually uses `Enumerable.Count`; its exact expected stdout is `2`.