title: What is the difference between using IEqualityComparer and Equals/GethashCode Override?
body: <p>When  i am using dictionaries sometimes I have to change the default Equals meaning in order to compare Keys. I see that if I override the Equals and GetHashCode on the key's class or i create a new class which implements IEqualityComparer I have the same result. So what's the difference between using IEqualityComparer and Equals/GethashCode Override?
  Two Examples:</p>
  
  <pre><code>class Customer
  {
      public string name;
      public int age;
      public Customer(string n, int a)
      {
          this.age = a;
          this.name = n;
      }
      public override bool Equals(object obj)
      {
          Customer c = (Customer)obj;
          return this.name == c.name &amp;&amp; this.age == c.age;
      }
      public override int GetHashCode()
      {
          return (this.name + ";" + this.age).GetHashCode();
      }
  }
    class Program
  {
      static void Main(string[] args)
      {
          Customer c1 = new Customer("MArk", 21);
          Customer c2 = new Customer("MArk", 21);
          Dictionary&lt;Customer, string&gt; d = new Dictionary&lt;Customer, string&gt;();
          Console.WriteLine(c1.Equals(c2));
          try
          {
              d.Add(c1, "Joe");
              d.Add(c2, "hil");
              foreach (KeyValuePair&lt;Customer, string&gt; k in d)
              {
                  Console.WriteLine(k.Key.name + " ; " + k.Value);
              }
          }
          catch (ArgumentException)
          {
              Console.WriteLine("Chiave già inserita in precedenza");
          }
          finally
          {
              Console.ReadLine();
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Second one :</p>
  
  <pre><code>class Customer
  {
      public string name;
      public int age;
      public Customer(string n, int a)
      {
          this.age = a;
          this.name = n;
      }
  }
  class DicEqualityComparer : EqualityComparer&lt;Customer&gt;
  {
      public override bool Equals(Customer x, Customer y) // equals dell'equalitycomparer
      {
          return x.name == y.name &amp;&amp; x.age == y.age;
      }
      public override int GetHashCode(Customer obj)
      {
          return (obj.name + ";" + obj.age).GetHashCode();
      }
  }
  class Program
  {
      static void Main(string[] args)
      {
          Customer c1 = new Customer("MArk", 21);
          Customer c2 = new Customer("MArk", 21);
          DicEqualityComparer dic = new DicEqualityComparer();
          Dictionary&lt;Customer, string&gt; d = new Dictionary&lt;Customer, string&gt;(dic);
          Console.WriteLine(c1.Equals(c2));
          try
          {
              d.Add(c1, "Joe");
              d.Add(c2, "hil");
              foreach (KeyValuePair&lt;Customer, string&gt; k in d)
              {
                  Console.WriteLine(k.Key.name + " ; " + k.Value);
              }
          }
          catch (ArgumentException)
          {
              Console.WriteLine("Chiave già inserita in precedenza");
          }
          finally
          {
              Console.ReadLine();
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Both examples have the same result.</p>
  
  <p>Thanks in advance.</p>