title: GetHashCode for a type holding string fields
body: <p>I have this class, where I have Object Equals overriden:</p>
  <pre><code>public class Foo
  {
      public string string1 { get; set; }
  
      public string string2 { get; set; }
  
      public string string3 { get; set; }
  
      public override bool Equals(object other)
      {
          if (!(other is Foo)) return false;
          Foo otherFoo = (other as Foo);
  
          return otherFoo.string1 == string1 &amp;&amp; otherFoo.string2 == string2 &amp;&amp; otherFoo.string3 == string3;
      }
  }
  </code></pre>
  <p>I get a warning &quot;overrides object.equals but not object.gethashcode&quot; of which I understand the need of overriding the GetHashCode, for my type to behave according to a hashable type.</p>
  <p>As far as I researched, for this code to be unique usually the XOR operator is used, or prime number multiplications are involved. So according to my sources, <a href="https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/336aedhh(v=vs.100)?redirectedfrom=MSDN" rel="nofollow noreferrer">source1</a> and <a href="https://sodocumentation.net/csharp/topic/3429/equals-and-gethashcode" rel="nofollow noreferrer">source2</a> I was considering this two options for my GesHashCode override method.</p>
  <p>1:</p>
  <pre><code>public override int GetHashCode() {
          return string1.GetHashCode() ^ string2.GetHashCode() ^ string3.GetHashCode();
  }
  </code></pre>
  <p>2:</p>
  <pre><code>public override int GetHashCode() {
          return (string1 + string2 + string3).GetHashCode();
  }
  </code></pre>
  <p>I am not sure either if this approaches ensures the purpose of the GetHashCode override in my case, that is to eliminate the compilation warning, and by the way ensure that the type can be handeled in collections properly, which I believe thas is that if the values they hold are equal to be considered equal, but if equal values on diferent instances take place in a collection, each needs to be found accordingly.</p>
  <p>In the case of both of the approaches being valid, I wonder which one might be better and why.</p>