In this toy code:
void Main()
{
var x = new string[] {"abc", "DEF"};
var y = new string[] {"ABC", "def"};
var c = new CompareCI();
var z = x.Except(y, c);
foreach (var s in z) Console.WriteLine(s);
}
private class CompareCI : IEqualityComparer<string>
{
public bool Equals(string x, string y)
{
return string.Equals(x, y, StringComparison.OrdinalIgnoreCase);
}
public int GetHashCode(string obj)
{
return obj.GetHashCode();
}
}
It seems like the Except method is ignoring my customer comparer. I get these results:
abc
DEF
Which looks like the case is not being ignored. Also, when I ran it under debug and put a breakpoint at the call to string.Equals in the Customer Comparer, the breakpoint never hit, although the code ran and I got the result I posted. i expected no results, since the sequences are equal if case is ignored.
Guess I'm doing something wrong, but I need a second pair of eyes to spot it.
.NET Framework already provides a StringComparer Class, that uses specific case and culture-based or ordinal comparison rules - so in this case there is no need to create a custom comparer.
This will work:
var x = new string[] { "abc", "DEF" };
var y = new string[] { "ABC", "def" };
var z = x.Except(y, StringComparer.OrdinalIgnoreCase);