0

THIS IS NOT A DUPE OF THIS LINKED QUESTION!!!!

That question is about using the content of an object (in particular the value of a specific field of an object as a key). This question is about using they reference itself. Identical content of 2 objects would still be unique entries in this example which IS NOT WHAT THE OTHER QUESTION IS ASKING!

I agree with kevin-krumwiede in the comments that this answer is the best way to make sure what I'm doing works in all situations.

I'm using objects as keys in a Dictionary in C#. I want to associate data with specific instances of those objects. Is this safe and supported or do I need to do more than just declare the object as the key?

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

    public class Program
    {
        public class Foo 
        {
        }

        public static void Main(string[] args)
        {
             Dictionary<Foo, string> dict = new Dictionary<Foo, string>();

             Foo a = new Foo();
             Foo b = new Foo();
             Foo c = new Foo();
             dict.Add(a, "bla");
             dict.Add(b, "bloop");

             Console.WriteLine("a in dict: " + dict.ContainsKey(a));  // true
             Console.WriteLine("b in dict: " + dict.ContainsKey(b));  // true
             Console.WriteLine("c in dict: " + dict.ContainsKey(c));  // false
             Console.WriteLine("a's data: " + dict[a]);  // bla
             Console.WriteLine("b's data: " + dict[b]);  // bloop
        }
    }

Seems to work. Any gotchas I need to be a aware of? Perf issues with using references as the key?

Note: There was this mis-labeled question that was titled "Using an object as a generic Dictionary key" but the question wasn't actually about using an Object as a key. It was about using the field of an object as a key. I fixed the title to make it clear this is a different question. I don't care what the data in the object is. I only care it's the same instance or not.

gman
  • 100,619
  • 31
  • 269
  • 393
  • No, https://stackoverflow.com/questions/634826/using-the-field-of-an-object-as-a-generic-dictionary-key is about your question – Backs Apr 03 '18 at 03:38
  • 2
    Are you worried about the objects overriding `Equals` to non-reference equality? I’m pretty sure that’s the only gotcha. – Ry- Apr 03 '18 at 03:40
  • you need to keep the object instance alive all the time (static), so that you can retrieve the value/ compare the key later on. I don't think there is any perf issue. – kurakura88 Apr 03 '18 at 03:48
  • The object will be alive by the fact that it's in the Dictionary right? – gman Apr 03 '18 at 03:48
  • @KevinKrumwiede, that's certainly related. Not sure it's a dupe. It shows that if Equals and/or GetHashCode exist on the given object then those will be used. It doesn't say whether or not using the reference itself as the key is safe/works/supported. – gman Apr 03 '18 at 03:51
  • Jon Skeet's answer shows how to do exactly what you want. – Kevin Krumwiede Apr 03 '18 at 03:52
  • @KevinKrumwiede, Jon Skeet's answer shows me how to make an object that has an impl for Equals and/or GetHashCode to do what I want. It does **not** answer the question of whether or not what I'm doing above works, is safe, and is supported. I suppose I could look at it as that solution will continue to work reguardless of how `Foo` is modified in the future though ;) – gman Apr 03 '18 at 03:56
  • 1
    @gman It tells you how to make a `Dictionary` that does what you want, that will treat its keys the way you want regardless of whether they override `Equals`. – Kevin Krumwiede Apr 03 '18 at 03:58

0 Answers0