I am trying to have a data structure with multiple string keys. To do this, I tried to create a Dictionary with string[] element. But the ContainsKey do no seem to work as I expect:
Dictionary<string[], int> aaa = new Dictionary<string[], int>();
int aaaCount = 0;
aaa.Add(new string[] { string1, string2 }, aaaCount++);
if (!aaa.ContainsKey(new string[] { string1, string2 }))
{
aaa.Add(new string[] { string1, string2 }, aaaCount++);
}
I see that there are two entries in aaa after the execution of the code above while I was expecting only one. Is this the expected behaviour? How can I ensure that there are no duplicate entries in the Dictionary?
Note: I tried the same with a list as well (List and the result is the same - the Contains method does not really work with string[])
If you want to use
string[]
as TKey, you should passIEqualityComparer<string[]>
to the constructor ofDictionary
. Because Otherwise Dictionary uses standard comparison forTKey
and in case ofstring[]
it just compares references hencestring[]
is reference type. You have to implement IEqualityComparer yourself. It can be done in the following way:(The implementation is quite naive, I provide it just as the starting point)