title: Using object references vs. identifiers as collection key
body: <p>is it safe to use an object referece as dictionary key or sould I use some identifier?</p>
  
  <p>Here is the scenario: suppose you have a class "MyClass" with it's own identfier. For the example think of a unique integer value. Now I want to lookup additional information for that class using a dictionary. I could specify the dictionary as Dictionary(of Integer, &lt;sometype&gt;)) or the use the object type itself as key. Then the dictionary would be Dictionary(of &lt;MyClass&gt;, &lt;sometype&gt;).</p>
  
  <p>I am not sure if the second approach is stable enough. I would assume that a key of type &lt;MyClass&gt; will translate to a reference pointing to the object specified as key. If so, then that will be an integer again (I would guess). With respect to performance both approaches should then be the same. But I am concerned that refrences could change due to memory compaction like mentioned <a href="https://stackoverflow.com/a/750958/342594">here</a>. If that is true, the reference stored in the dictionary would be invalid. So in one case I would find a certain dictionary item and in another case after the memory compaction I would fail to find the same object because its reference changed. (I don't know how to explain this any shorter, but I hope, you get the point.)</p>
  
  <p>I found a similar question <a href="https://stackoverflow.com/questions/47067047/when-do-i-keep-a-mapidentifier-object-vs-a-collectionobject-with-identifier">here</a>, but that doesn't answer my question.
  If I missed an existing question, please kindly post a link. </p>
  
  <p>Thanks,
  Sascha</p>