[C # .NET] [VB.Net] Practice IEqualityComparer is not collimated

[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;
 }
 }
 } 

因为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 class
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);
}

当然也可以用CollectionsUtilThe class is not case sensitive, this program looks much shorter. Share picture

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

Share pictures

 Share pictures 2010~2017 C# Fourth Season

Original: Large column [C#.NET][VB.NET] Practice IEqualityComparer regardless of size Write

Leave a Comment

Your email address will not be published.