[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

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3089 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.