Let us Learn Tuple-Part4 ( Create MultiKey dictionary using Tuple)

Rajnilari2015
Posted by in C# category on for Beginner level | Points: 250 | Views : 776 red flag

In this fourth part, we will look into how to Create MultiKey dictionary using Tuple


 Download source code for Let us Learn Tuple-Part4 ( Create MultiKey dictionary using Tuple)

Recommendation
Read Let us Learn Tuple-Part3 ( Tuple and Arrays) before this article.

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.

Recommendation
Read Let us Learn Tuple-Part5 ( LAMBDA with Tuple) after this article.
Page copy protected against web site content infringement by Copyscape

About the Author

Rajnilari2015
Full Name: Niladri Biswas (RNA Team)
Member Level: Platinum
Member Status: Member,MVP
Member Since: 3/17/2015 2:41:06 AM
Country: India
-- Thanks & Regards, RNA Team


Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)