title: C# - Generic HashCode implementation for classes
body: <p>I'm looking at how build the best HashCode for a class and I see some algorithms. I saw this one : <a href="https://stackoverflow.com/questions/113511/hash-code-implementation">Hash Code implementation</a>, seems to be that .NET classes HashCode methods are similar (see by reflecting the code).</p>
  
  <p>So question is, why don't create the above static class in order to build a HashCode automatically, just by passing fields we consider as a "key".</p>
  
  <pre><code>// Old version, see edit
  public static class HashCodeBuilder
  {
      public static int Hash(params object[] keys)
      {
          if (object.ReferenceEquals(keys, null))
          {
              return 0;
          }
  
          int num = 42;
  
          checked
          {
              for (int i = 0, length = keys.Length; i &lt; length; i++)
              {
                  num += 37;
                  if (object.ReferenceEquals(keys[i], null))
                  { }
                  else if (keys[i].GetType().IsArray)
                  {
                      foreach (var item in (IEnumerable)keys[i])
                      {
                          num += Hash(item);
                      }
                  }
                  else
                  {
                      num += keys[i].GetHashCode();
                  }
              }
          }
  
          return num;
      }
  }
  </code></pre>
  
  <p>And use it as like this :</p>
  
  <pre><code>// Old version, see edit
  public sealed class A : IEquatable&lt;A&gt;
  {
      public A()
      { }
  
      public string Key1 { get; set; }
      public string Key2 { get; set; }
      public string Value { get; set; }
  
      public override bool Equals(object obj)
      {
          return this.Equals(obj as A);
      }
  
      public bool Equals(A other)
      {
          if(object.ReferenceEquals(other, null)) 
              ? false 
              : Key1 == other.Key1 &amp;&amp; Key2 == other.Key2;
      }
  
      public override int GetHashCode()
      {
          return HashCodeBuilder.Hash(Key1, Key2);
      }
  }
  </code></pre>
  
  <p>Will be much simpler that always is own method, no? I'm missing something?</p>
  
  <hr>
  
  <h2>EDIT</h2>
  
  <p>According all remarks, I got the following code :</p>
  
  <pre><code>public static class HashCodeBuilder
  {
      public static int Hash(params object[] args)
      {
          if (args == null)
          {
              return 0;
          }
  
          int num = 42;
  
          unchecked
          {
              foreach(var item in args)
              {
                  if (ReferenceEquals(item, null))
                  { }
                  else if (item.GetType().IsArray)
                  {
                      foreach (var subItem in (IEnumerable)item)
                      {
                          num = num * 37 + Hash(subItem);
                      }
                  }
                  else
                  {
                      num = num * 37 + item.GetHashCode();
                  }
              }
          }
  
          return num;
      }
  }
  
  
  public sealed class A : IEquatable&lt;A&gt;
  {
      public A()
      { }
  
      public string Key1 { get; set; }
      public string Key2 { get; set; }
      public string Value { get; set; }
  
      public override bool Equals(object obj)
      {
          return this.Equals(obj as A);
      }
  
      public bool Equals(A other)
      {
          if(ReferenceEquals(other, null))
          {
              return false;
          }
          else if(ReferenceEquals(this, other))
          {
              return true;
          }
  
          return Key1 == other.Key1
              &amp;&amp; Key2 == other.Key2;
      }
  
      public override int GetHashCode()
      {
          return HashCodeBuilder.Hash(Key1, Key2);
      }
  }
  </code></pre>