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

Viewed 3194

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?

3 Answers

you have to use the extension method Contains that is part of the namespace System.Linq. Just add this line on top of your file and the method with two arguments should be available.

using System.Linq; 

Note that the overloaded method is only available on target Win 10 version 1809 and higher.

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.

Related