Does this code depend on string interning to work?
- by Nick Gotch
I'm creating a key for a dictionary which is a structure of two strings. When I test this method in a console app, it works, but I'm not sure if the only reason it works is because the strings are being interned and therefore have the same references.
Foo foo1 = new Foo();
Foo foo2 = new Foo();
foo1.Key1 = "abc";
foo2.Key1 = "abc";
foo1.Key2 = "def";
foo2.Key2 = "def";
Dictionary<Foo, string> bar = new Dictionary<Foo, string>();
bar.Add(foo1, "found");
if(bar.ContainsKey(foo2))
System.Console.WriteLine("This works.");
else
System.Console.WriteLine("Does not work");
The struct is simply:
public struct Foo
{
public string Key1;
public string Key2;
}
Are there any cases which would cause this to fail or am I good to rely on this as a unique key?