c# - How can I access a Dictionary through a key hashcode? -


i have dictionary that:

 dictionary<mycompositekey, int> 

clearly mycompositekey class designed implements iequalitycomparer , has gethashcode method.

as far know, dictionary uses key's hash access value, here's question:

while can access value via dict.trygetvalue(new mycompositekey(params)), wanted rid off new overhead on each access.

for reason wondering if there's way access value directly key's hash (which can compute lower overhead).

there no way that.

note hash collisions may occur, there many keys in dictionary<,> matching given hash. need equals find out (if any) correct.

you talk new overhead. sure significant in case?

if is, consider making mycompositekey immutable struct instead of class. might faster in cases, eliminating need garbage collector remove "loose" keys memory.

if mycompositekey struct, expression new mycompositekey(params) loads params onto call stack (or cpu registers or whatever run-time figures best).


addition: if go struct, consider implementing iequatable<>. this:

struct mycompositekey : iequatable<mycompositekey> {   // readonly fields/properties    public override bool equals(object obj)   {     if (obj mycompositekey)       return equals((mycompositekey)obj); // unbox , go below overload      return false;   }    public bool equals(mycompositekey other) // implements interface, can avoid boxing   {     // equality logic here   }    public override int gethashcode()   {     // hash logic here   } } 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -