Generic Dictionary and generating a hashcode for multi-part key

Posted by Andrew on Stack Overflow See other posts from Stack Overflow or by Andrew
Published on 2010-03-22T22:15:15Z Indexed on 2010/03/22 22:31 UTC
Read the original article Hit count: 460

Filed under:
|

I have an object that has a multi-part key and I am struggling to find a suitable way override GetHashCode. An example of what the class looks like is.

public class wibble{
   public int keypart1 {get; set;}
   public int keypart2 {get; set;}
   public int keypart3 {get; set;}
   public int keypart4 {get; set;}
   public int keypart5 {get; set;}
   public int keypart6 {get; set;}
   public int keypart7 {get; set;}
   public single value {get; set;}
}

Note in just about every instance of the class no more than 2 or 3 of the keyparts would have a value greater than 0.

Any ideas on how best to generate a unique hashcode in this situation?

I have also been playing around with creating a key that is not unique, but spreads the objects evenly between the dictionaries buckets and then storing objects with matched hashes in a List<> or LinkedList<> or SortedList<>.
Any thoughts on this?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about collections