Is there an easy way to make a hash key for a class based on it's data??? Or some interface for that?
I have a Dictionary<MyClass, int>
.
MyClass is very simple, it contains a name and a string array:
class MyClass
{
public string Name {get; private set;}
public string[] Attributes {get; private set; }
//and a constructor and some methods
}
Now, if I have two MyClass
instances containing same names and attributes, I need that dictionary to consider as if they have same key.
I tried to make it a struct, and tried to make a Dictionary<string[], int>
as well, but all three cases are equal, dicionary sees different keys for every instance, even if with same data.
I could create a string key with an algorithm taking name and all parameters, and create a Dictionary<string, int>
, but I'd like something more automatic. Is there any other way?
Maybe an interface (that would't avoid an algorythm, but is better than nothing).