title: What's the role of GetHashCode in the IEqualityComparer<T> in .NET?
body: <p>I'm trying to understand the role of the GetHashCode method of the interface IEqualityComparer.</p>
  
  <p>The following example is taken from MSDN:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  class Example {
      static void Main() {
          try {
  
              BoxEqualityComparer boxEqC = new BoxEqualityComparer();
  
              Dictionary&lt;Box, String&gt; boxes = new Dictionary&lt;Box,
                                                  string&gt;(boxEqC);
  
              Box redBox = new Box(4, 3, 4);
              Box blueBox = new Box(4, 3, 4);
  
              boxes.Add(redBox, "red");
              boxes.Add(blueBox, "blue");
  
              Console.WriteLine(redBox.GetHashCode());
              Console.WriteLine(blueBox.GetHashCode());
          }
          catch (ArgumentException argEx) {
  
              Console.WriteLine(argEx.Message);
          }
      }
  }
  
  public class Box {
      public Box(int h, int l, int w) {
          this.Height = h;
          this.Length = l;
          this.Width = w;
      }
      public int Height { get; set; }
      public int Length { get; set; }
      public int Width { get; set; }
  }
  
  class BoxEqualityComparer : IEqualityComparer&lt;Box&gt; {
  
      public bool Equals(Box b1, Box b2) {
          if (b1.Height == b2.Height &amp; b1.Length == b2.Length
                              &amp; b1.Width == b2.Width) {
              return true;
          }
          else {
              return false;
          }
      }
  
      public int GetHashCode(Box bx) {
          int hCode = bx.Height ^ bx.Length ^ bx.Width;
          return hCode.GetHashCode();
      }
  }
  </code></pre>
  
  <p>Shouldn't the Equals method implementation be enough to compare two Box objects? That is where we tell the framework the rule used to compare the objects. Why is the GetHashCode needed?</p>
  
  <p>Thanks.</p>
  
  <p>Lucian</p>