title: HashSet with a custom struct allocates heavy with Contains function
body: <p>I am using the <code>HashSet</code> collection type which has already significantly improved the performance of my algorithm. It seems that each time I invoke <code>myHashSet.Contains(someValue)</code> the internal implementation is boxing the value type immediately before invoking <code>Equals</code>.</p>
  
  <p>Is there a way to avoid these wasteful allocations when using value types?</p>
  
  <p><strong>Sample Code:</strong></p>
  
  <pre><code>public struct TestStruct {
      public int a;
      public int b;
  
      public override int GetHashCode() {
          return a ^ b;
      }
  
      public override bool Equals(object obj) {
          if (!(obj is TestStruct))
              return false;
          TestStruct other = (TestStruct)obj;
          return a == other.a &amp;&amp; b == other.b;
      }
  }
  
  var hashset = new HashSet&lt;TestStruct&gt;();
  PopulateSet(hashset);
  
  // About to go crazy on the allocations...
  if (hashset.Contains(someValue)) { ... }
  // Lots of allocations just happened :(
  </code></pre>