获取通用列表项的计数

ahm*_*iee 3 c# generics generic-list c#-4.0

我有一个通用的int列表,List<int>()我想知道它的一个项目的计数.我该怎么做?

ojl*_*ecd 13

使用扩展方法是最简单的方法

int count = list.Count(i => i > 10);// get the count of those which is bigger than 10
Run Code Online (Sandbox Code Playgroud)

  • @ahmadalishafiee - `Count`依赖于`using System.Linq;`. (2认同)

Fen*_*ton 6

如果要计算列表中项目数的计数,可以使用表达式执行此操作:

int count = myList.Count(i => i.Equals(5));
Run Code Online (Sandbox Code Playgroud)