Do we have a "Contains" method in IEnumerable

c#

Solution

Do you really implement the non-generic `IEnumerable`, or the generic `IEnumerable<T>`? If you can possibly implement the generic one, your life will become a lot simpler - as then you can use LINQ to Objects, which does indeed have a `Contains` extension method.

Otherwise, you could potentially convert from the non-generic to generic using `Cast` or `OfType`, e.g.

bool found = nonGeneric.Cast<TargetType>().Contains(targetItem);

It would be nicer if you just implemented the generic interface to start with though :)

Problem

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 something in that list?

Original source