查找列表中特定项目的计数

Vis*_*hnu 3 c#

我有List<String>1000个名字.我想找出以字母"S"开头的名字数.

什么是最好的选择呢?

Bob*_*ale 8

如果Linq可用

using System.Linq;
list.Where(s=>s!=null && s.StartsWith("S")).Count();
Run Code Online (Sandbox Code Playgroud)

如果Linq不可用.

int count=0;
foreach (string s in list) {
  if (s!=null && s.StartsWith("S")) count++;
}
Run Code Online (Sandbox Code Playgroud)