title: How should mutable dictionary values be tested for equality?
body: <p>I have the following class:</p>
  
  <pre><code>public class Foo
  {
      public Guid Id { get; set; }
      public string Bar { get; set; }
      public DateTime Baz { get; set; }
  }
  </code></pre>
  
  <p>And I want to test it for equality in a Dictionary like so:</p>
  
  <pre><code>var dict = new Dictionary&lt;Guid, Foo&gt;();
  // Add some foos...
  if (dict.ContainsValue(myFoo))
  {
      // Do something...
  }
  </code></pre>
  
  <p>Where ContainsValue should return true for Foos where the Bar and Baz properties are equal to one already in the dictionary.</p>
  
  <p>The ContainsValue <a href="https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.containsvalue?view=netcore-2.2" rel="nofollow noreferrer">documentation</a> states that the default EqualityComparer is used. This suggests that I can implement the IEquatable interface and override the default Equals implementation like so:</p>
  
  <pre><code>public override bool Equals(Foo obj)
  {
          return obj != null &amp;&amp;
                 Id.Equals(obj.Id) &amp;&amp;
                 Bar == obj.Bar &amp;&amp;
                 Baz == obj.Baz;
  }
  </code></pre>
  
  <p>This <a href="https://blogs.msdn.microsoft.com/jaredpar/2009/01/15/if-you-implement-iequatablet-you-still-must-override-objects-equals-and-gethashcode/" rel="nofollow noreferrer">blog post</a> from MSDN suggests that when implementing IEquatable, one must also override the Equals(object) and GetHashCode() functions.</p>
  
  <p>However, if I were to use the same Equality logic for the Equals and GetHashCode functions, the object's hashcode would be based on mutable fields which is recommended against in this <a href="https://stackoverflow.com/a/873668/4677588">StackOverflow answer</a>.</p>
  
  <p>So therefore, my question:</p>
  
  <p>How should we define equality logic for mutable objects in cases like the one shown above whilst avoiding the problems inherent in basing an object's hashcode on mutable values?</p>