In this fourth part, we will look into how to Create MultiKey dictionary using Tuple
Introduction
This is the fourth part of the series Let us Learn Tuple.In this fourth part, we will look into how to Create MultiKey dictionary using Tuple
You can read the entire series of article as listed down
Let us Learn Tuple-Part1
Let us Learn Tuple-Part2 ( Creation of Generic Collections and Accessing the same using Tuple )
Let us Learn Tuple-Part3 ( Tuple and Arrays )
Let us Learn Tuple-Part4 ( Create MultiKey dictionary using Tuple )
Let us Learn Tuple-Part5 ( LAMBDA with Tuple )
Let us Learn Tuple-Part6 (Tuple Serialization and DeSerialization)
Let us Learn Tuple-Part7 (Last but not the least part)
Using the code
Tuple along with a Dictionary can serve the purpose of a MultiKey or Composite key as shown below
//declare a dictionary object that can hold multiple keys
var multiKeyDictionary = new Dictionary<Tuple<string, string>, string>();
//add some data to that
Enumerable
.Range(1, 10)
.ToList()
.ForEach
(
i => multiKeyDictionary.Add(
new Tuple<string, string>("100" + i, "900000000" + i) //multi-key or composite-key
, "User" + i // the values
)
);
The output will be

From the above we can figure out that we have created a multikey combination using tuple. Below way will demonstrate how to access the dictionary
//Access the dictionary
var keyToSearch= new Tuple("1001", "9000000001");
Console.WriteLine($" for the key : {keyToSearch}, the value : {multiKeyDictionary[keyToSearch]}");
Conclusion
In this article we learnt about Creation of MultiKey dictionary using Tuple. Hope this will be helpful. More to come in the following series. Zipped file attached.