[C#.NET][VB.NET] Practice IEqualityComparer regardless of case
PracticeIEqualityComparer, case-insensitive comparison
//Practice< /pre>public class InComparer: IEqualityComparer{CaseInsensitiveComparer myComparer = new CaseInsensitiveComparer();public int GetHashCode(object obj)pre>
{return obj.ToString().ToLowerInvariant().GetHashCode();}public new bool Equals(object x , object y){if (myComparer.Compare(x, y) == 0){return true ;}else{return false;}}} pre>
因为No distinction is made between uppercase and lowercase, so First and first are considered the same, and exceptions will occur when the program is executed.
//reference classHashtable 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);}当然也可以用 strong>CollectionsUtilThe class is not case sensitive, this program looks much shorter.
Hashtable myColl = CollectionsUtil. CreateCaseInsensitiveHashtable();myColl.Add("A", "1234");myColl.Add("a", "1234");If there is any error, please let me know, and novices please bear with me when posting
p>
2010~2017 C# Fourth Season
Original: Large column [C#.NET][VB.NET] Practice IEqualityComparer regardless of size Write