Search code examples
c#listcontains

No Overload for method 'Contains' takes 2 arguments with list and simple string


Borrowing from the example here, I tried to do the following:

List<string> animals = new List<string> { "Horse", "Cat", "Dog" };
bool testCase = animals.Contains("horse", StringComparer.CurrentCultureIgnoreCase);

But just doing that, I get "No overload for method 'Contains' takes 2 arguments".

I also tried it as:

List<string> animals = new List<string> { "Horse", "Cat", "Dog" };
string testAnimal = "horse";
bool testCase = animals.Contains(testAnimal, StringComparer.CurrentCultureIgnoreCase);
testCase = animals.Contains((string)testAnimal, StringComparer.CurrentCultureIgnoreCase);

But both of those get the same error.

what am I missing here?


Solution

  • You are probably searching for the Linq-extension method Contains<TSource>(IEnumerable<TSource>, TSource, IEqualityComparer<TSource>) which is documented here.

    In your case, it is probably sufficient to just add using System.Linq; to the top of your source file and the method pops up.