Do we have a “Contains” method in IEnumerable

前端 未结 3 1426
挽巷
挽巷 2021-01-11 09:41

I have a class in my code that is already deriving from IEnumerable. I was wondering if there is a way that I can use a \"Contains\" method on its instnaces to look for a so

3条回答
  •  旧巷少年郎
    2021-01-11 10:44

    public static bool Contains(this IEnumerable source, T value)
        {
            foreach (var i in source)
            {
                if (Equals(i, value))
                    return true;
            }
            return false;
        }
    

    If you want, you can add custom comparer as parameter ti extension method Contains

提交回复
热议问题