Dictionary<> Keys do not get fetched properly
NamespacedKey is just a class that stores 3 strings.
I override the GetHashCode() method in it to
Ive tried different hashcodes incase im just stupid but nothing works
7 Replies
using the string of domain + key + family as the key in the dictionary works, but not my own class
Okay apparently you need to override the equals function for some reason? I'd assume it just fetches the key from the hash code alone but it does an equals check aswell?
indeed it does: https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs,428
you should pass an EqualityComparer instead of implementing GetHashCode and Equals
and if you do implement GetHashCode, use
HashCode.Combine
Implementing an EqualityComparer seems unnecessary to me in this case.
Tip for OP: in VS you can generate GetHashCode and Equals implementations using the quick actions context menu when opened on the type name (put caret in type name, press ctrl+. then select generate Equals+GetHashCode)
how do you implement
GetHashCode
and Equals
for byte[]
?Alternatively, if the identity of a NamespaceKey is fully determined by those 3 strings, you should look at records also.
wrong channel...
sorry
you're right
Records come with generated Equals/GetHashCode implementations that should fit your needs (from what I can tell frkm your examples)