你可以使用一点Linq:
var result = text.GroupBy(c => c)
.Select(g => new { symbol = g.Key, times = g.Count() });
foreach (var t in result)
{
Console.WriteLine("Symbol {0} is met {1} times",t.symbol,t.times);
}
Run Code Online (Sandbox Code Playgroud)
或者更简单
var result = text.GroupBy(c => c, (c, g) => new { symbol = c, times = g.Count() });
foreach (var t in result)
{
Console.WriteLine("Symbol {0} is met {1} times",t.symbol,t.times);
}
Run Code Online (Sandbox Code Playgroud)