Let's say your class looks like this:
class Frob { public string Foo { get; set; } public int Bar { get; set; } public double FooBar { get; set; } }
Let's say you define equals so that two Frob instances are equal if their Foo and their Bar are equal, but FooBar doesn't matter.
Then you must define GetHashCode in terms of Foo and Bar . One of the methods:
return this.Foo.GetHashCode() * 17 + this.Bar.GetHashCode();
Basically, you just want to include all the fields that go into the definition of equality. One way is to just keep accumulating and multiplying by 17, as I did. It is fast, simple, correct and usually gives a good spread.
jason Jan 25 2018-12-12T00: 00Z
source share