30秒学会 C# 片段 · 2020年3月2日

30秒学会 C# 片段 – CountOccurences

Counts the occurences of a value in a collection.

Use IEnumerable.Count() in combination with EqualityComparer<T>.Default.Equals() to compare each value in the IEnumerable with el.

代码实现

using System.Collections.Generic;
using System.Linq;

public static partial class _30s 
{
  public static int CountOccurences<T>(IEnumerable<T> obj, T el) 
  {
    return obj.Count(f => EqualityComparer<T>.Default.Equals(f, el));
  }
}

使用样例

string s = "fooBar";
List<int> nums = new List<int> { 1, 2, 3, 3, 3, 4, 5, 6 };

_30s.CountOccurences(s,'o'); // 2
_30s.CountOccurences(nums,3); // 3