Count 和 LongCount

Count 返回 IEnumerable<T> 中的元素数。Count 还公开了一个可选的谓词参数,允许你过滤要计数的元素。

int[] array = { 1, 2, 3, 4, 2, 5, 3, 1, 2 };

int n = array.Count(); // returns the number of elements in the array
int x = array.Count(i => i > 2); // returns the number of elements in the array greater than 2

LongCount 的工作方式与 Count 相同,但返回类型为 long,用于计算比 tihuan 更长的 IEnumerable<T> 序列 8

int[] array = GetLargeArray();

long n = array.LongCount(); // returns the number of elements in the array
long x = array.LongCount(i => i > 100); // returns the number of elements in the array greater than 100