How can I make Array.Contains case-insensitive on a string array?

Viewed 95961

I am using the Array.Contains method on a string array. How can I make that case-insensitive?

4 Answers
array.Contains("str", StringComparer.OrdinalIgnoreCase);

Or depending on the specific circumstance, you might prefer:

array.Contains("str", StringComparer.CurrentCultureIgnoreCase);
array.Contains("str", StringComparer.InvariantCultureIgnoreCase);

Implement a custom IEqualityComparer that takes case-insensitivity into account.

Additionally, check this out. So then (in theory) all you'd have to do is:

myArray.Contains("abc", ProjectionEqualityComparer<string>.Create(a => a.ToLower()))
new[] { "ABC" }.Select(e => e.ToLower()).Contains("abc") // returns true
Related