4

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?

TulsaNewbie
  • 382
  • 3
  • 15
  • Does this answer your question? [Searching a List for an EXACT case insenitive match](https://stackoverflow.com/questions/35951701/searching-a-liststring-for-an-exact-case-insenitive-match) – Shachaf.Gortler Mar 02 '20 at 22:31
  • No. The problem was that I didn't have system.linq, and that page doesn't mention it at all. – TulsaNewbie Mar 02 '20 at 22:33

3 Answers3

0

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.

Max Play
  • 3,717
  • 1
  • 22
  • 39
  • My system doesn't have the linq namespace... and I'm following the steps athttps://stackoverflow.com/questions/37694861/the-type-or-namespace-name-linq-does-not-exist-in-the-namespace-system and I don't even see system.core or system.data.linq – TulsaNewbie Mar 02 '20 at 22:27
  • 1
    there we go... somehow my target framework was set to 2.0 ... yeesh... – TulsaNewbie Mar 02 '20 at 22:28
  • 1
    Unless you are on a really old version of .net Framework (< 3.5(, you must have System.Linq available. – Claudio Valerio Mar 02 '20 at 22:31
0

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; 
claudiom248
  • 346
  • 2
  • 9
0

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

Greg
  • 1
  • 3