You can use dictionaries with tuples as the keys or values. I’ll show examples below.
Dictionary with tuple as key
Tuples have two or more fields, so using a tuple as the key is a simple way to have compound keys in a dictionary.
Here’s an example of creating a dictionary with a named tuple as the key:
using System.Collections.Generic;
var dictionary = new Dictionary<(string Name, int Age), string>();
Code language: C# (cs)
You can use this dictionary just like any other dictionary. The only difference is you have to use a tuple when referring to the key.
Here’s an example of adding items to the dictionary and passing in tuples for the keys (i.e. (“Bob”, 38)):
dictionary.Add(("Bob", 38), "Burgers");
dictionary.Add(("Bob", 40), "Fries");
//Use field names (Name: and Age:) for clarity
dictionary.Add((Name:"Teddy", Age:45), "Burgers");
Code language: C# (cs)
When you want to get a value by its key, you have to specify the tuple. Here’s an example:
var val = dictionary[("Teddy", 45)];
Console.WriteLine($"Teddy, 45, likes {val}"); //Outputs: Teddy, 45, likes Burgers
Code language: C# (cs)
Dictionary of tuples vs nested dictionaries
When you want a compound key, you can either use a tuple (as shown above) or use a nested dictionary. For comparison, take a look at the difference in creating, adding, and looking up values in a tuple dictionary vs a nested dictionary:
//Creating dictionary
var tupleDictionary = new Dictionary<(string Name, int Age), string>();
var nestedDictionary = new Dictionary<string, Dictionary<int, string>>();
//Adding items to the tuple dictionary
tupleDictionary.Add(("Bob", 38), "Burgers");
tupleDictionary.Add(("Bob", 40), "Fries");
//Adding items to the nested dictionary
nestedDictionary.Add("Bob", new Dictionary<int, string>() { [38] = "Burgers" });
nestedDictionary["Bob"].Add(40, "Fries");
//Looking up a value in the tuple dictionary
var val = tupleDictionary[("Bob", 38)];
//Looking up a value in the nested dictionary
var val2 = nestedDictionary["Bob"][38];
Code language: C# (cs)
Tuples are simpler than using nested dictionaries, and also tend to provide better performance (up to 2x faster in my testing with 100k items. Definitely do your own perf testing if you have doubts).
Dictionary with tuple as value
You can use tuples as dictionary values. This is useful when you want to store multiple fields and don’t want to create a tiny container class.
Here’s an example of creating and using a dictionary with named tuple values:
using System.Collections.Generic;
var dictionary = new Dictionary<string, (string Food, int Age)>();
dictionary.Add("Bob", ("Burgers", 40));
dictionary.Add("Teddy", ("Burgers", 45));
foreach(var kvp in dictionary)
{
Console.WriteLine($"{kvp.Key}, age {kvp.Value.Age} likes {kvp.Value.Food}");
}
Code language: C# (cs)
This outputs the following:
Bob, age 40 likes Burgers
Teddy, age 45 likes Burgers
Code language: plaintext (plaintext)
Notice that it’s really beneficial to use named tuples here. When you get the tuple value from the dictionary, you can refer to the field names (instead of Item1 and Item2 for example).