[C # .NET] [VB.NET] practice IEqualityComparer not case sensitive
Practice the IEqualityComparer , case-insensitive comparison
//practice
public class InComparer : IEqualityComparer
{
CaseInsensitiveComparer myComparer = new CaseInsensitiveComparer();
public int GetHashCode(object obj)
{
return obj.ToString().ToLowerInvariant().GetHashCode();
}
public new bool Equals(object x, object y)
{
if (myComparer.Compare(x, y) == 0)
{
return true;
}
else
{
return false;
}
}
}
First because no case so as identical with the first, the program exception occurs during execution.
// class references
Hashtable myData = new Hashtable(new InComparer());
myData.Add("First", "1st");
myData.Add("Senond", "2nd");
myData.Add("Third", "3rd");
myData.Add("Fourth", "4th");
myData.Add("Fifth", "5th");
myData.Add("first", "1st");
foreach (DictionaryEntry myEntry in myData)
{
Console.WriteLine("{0} = {1}", myEntry.Key, myEntry.Value);
}
Of course, you can also use CollectionsUtil class is not case sensitive, this program looks much shorter.
Hashtable myColl = CollectionsUtil.CreateCaseInsensitiveHashtable();
myColl.Add("A", "1234");
myColl.Add("a", "1234");
If any error, please notify the novice posting him to bear
2010 ~ 2017 C # in the fourth quarter
Original: Large column [C # .NET] [VB.NET] practice IEqualityComparer not case sensitive