Search code examples
c#binning

C# GetHashCode for binning


I would like to bin a set of objects, in particular Point types with an x and y value so that I can count the number of points at each x, y pair that exists in the set of Points. The max value of x and y is [-1000,1000] so it is easy enough to generate a unique hash code for each possible point.

For the actual binning process, can this be done using a Dictionary. Does the GetHashCode get used for this when I add/lookup a Point to the dictionary?

Is there a better way to do binning?


Solution

  • Yes, unless you pass a custom instance to provide hash codes and check equality when you construct the Dictionary instance, GetHashCode and Equals methods for your type will be used.

    To provide your custom hashing mechanism, you can simply override GetHashCode and Equals.