Compare Objects in C# using Hash Code

My previous blog  compared objects in C#, but in this blog I will provide the code to compare using Hash code. Now hash code does not guarantee complete unique hash value so we will use slight modification.

See this Code 
  1. using System;  
  2. namespace ConsolePractice  
  3. {  
  4.     public class Test  
  5.     {  
  6.         public int Value { getset; }  
  7.         public string String1 { getset; }  
  8.         public string String2 { getset; }  
  9.   
  10.   
  11.   
  12.         public override int GetHashCode()  
  13.         {  
  14.             int hash = 19;  
  15.             hash = hash * 31 + Value;  
  16.             hash = hash * 31 + String1.SafeGetHashCode();  
  17.             hash = hash * 31 + String2.SafeGetHashCode();  
  18.             return hash;  
  19.         }  
  20.         public override bool Equals(object obj)  
  21.         {  
  22.             Test test = obj as Test;  
  23.             if (test== null)  
  24.             {  
  25.                 return false;  
  26.             }  
  27.             return Value == test.Value &&  
  28.                 String1 == test.String1 &&  
  29.                 String2 == test.String2;  
  30.         }  
  31.     }  
  32.   
  33.     class Demo  
  34.     {  
  35.         static void Main()  
  36.         {  
  37.             Test p1 = new Test  
  38.             {  
  39.                 Value = 10,  
  40.                 String1 = "Test1",  
  41.                 String2 = "Test2"  
  42.             };  
  43.             Test p2 = new Test  
  44.             {  
  45.                 Value = 10,  
  46.                 String1 = "Test1",  
  47.                 String2 = "Test2"  
  48.             };  
  49.             bool areEqual = p1.Equals(p2);  
  50.   
  51.             Console.WriteLine(areEqual.ToString());  
  52.             Console.ReadLine();  
  53.   
  54.         }  
  55.     }  

  56. }  
You need to provide "SafeGetHashCode" as an extension method in another utility class. 
  1. namespace ConsolePractice  
  2. {  
  3.     static class utility  
  4.     {  
  5.         public static int SafeGetHashCode<T>(this T value) where T : class  
  6.         {  
  7.             return value == null ? 0 : value.GetHashCode();  
  8.         }  
  9.     }  
  10. }  
We checked the value equality of two objects using hash code. What about reference equality?

Reference quality can checked using object.Equals (or any other way you want). 

Note

There are many other ways such as IEqualityComparer<Thing>, check the code below:

Code
  1. using System;  
  2.   
  3. using System.Collections.Generic;  
  4.   
  5.   
  6. class ThingEqualityComparer : IEqualityComparer<Thing>  
  7. {  
  8.     public bool Equals(Thing x, Thing y)  
  9.     {  
  10.         if (x == null || y == null)  
  11.             return false;  
  12.   
  13.         return (x.Id == y.Id && x.Name == y.Name);  
  14.     }  
  15.   
  16.     public int GetHashCode(Thing obj)  
  17.     {  
  18.         return obj.GetHashCode();  
  19.     }  
  20. }  
  21.   
  22.   
  23. public class Thing  
  24. {  
  25.     public int Id { getset; }  
  26.     public string Name { getset; }  
  27.   
  28. }  
  29. class Demo  
  30. {  
  31.     static void Main()  
  32.     {  
  33.         Thing p1 = new Thing  
  34.         {  
  35.             Id = 10,  
  36.             Name = "Test1",  
  37.   
  38.         };  
  39.         Thing p2 = new Thing  
  40.         {  
  41.             Id = 10,  
  42.             Name = "Test1",  
  43.   
  44.         };  
  45.   
  46.         var comparer = new ThingEqualityComparer();  
  47.         Console.WriteLine(comparer.Equals(p1, p2));  
  48.   
  49.   
  50.         Console.ReadLine();  
  51.   
  52.     }  
  53. }  
Disclaimer

I have written this blog by researching some concepts and code from various sources. Jon Skeet,Eric Lippert and many others inspire me to learn.
Ebook Download
View all
Learn
View all