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