Am Facing a problem in Dictionaries. Whether an Array can be a Key of a Value???
Dictionary<string[], int> di = new Dictionary<string[], int>();
di.Add(new string[]
{
"1","2"
}, 1);
di.Add(new string[]
{
"2","3"
}, 2);
MessageBox.Show(di[new string[] { "2", "3" }].ToString()); // Here KeyNotFoundException occurred.
Why Exception?
By default only references of the arrays would be compared, so you either have to
IEqualityComparer<string[]> orTuple<string, string> as key instead ( since you only have two strings)Here's a similar question's answer which shows how to create a custom comparer for the Dictionary- constructor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With