Let us Learn Tuple-Part2 ( Creation of Generic Collections and Accessing the same using Tuple )

Rajnilari2015
Posted by in C# category on for Beginner level | Points: 250 | Views : 2810 red flag
Rating: 5 out of 5  
 1 vote(s)

In this previous article we have started our introductory journey with Tuple.In this second part we will look into creation of Generic Collections and Accessing the same using Tuple


 Download source code for Let us Learn Tuple-Part2 ( Creation of Generic Collections and Accessing the same using Tuple )

Recommendation
Read Let us Learn Tuple-Part1 before this article.

Introduction

This is the second of the series of articles Let us Learn Tuples. In this second part we will look into creation of Generic Collections and Accessing the same 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)

Let us create a collection of studentTuple as under.

  1. By creating a class instance of Tuple Class

    E.g.

              List<Tuple<string, int, List<string>>> studentTupleInstances = new List<Tuple<string, int, List<string>>>();
    
              //Add the first record
            studentTupleInstances.Add(new Tuple<string, int, 
              List<string>>(
                "Rajlakshmi Biswas", 4, 
       new List<string> { "Mathematics","English", "Science and Social Science","Hindi"}));
    
            //Add the second record
            studentTupleInstances.Add(new Tuple<string, int, 
            List<string>>(
           "Niladri Biswas"
           , 36
    , new List<string> { "Mathematics","English","Computer Science","History","Physics"}));
    
          //Add the third record
          studentTupleInstances.Add(new Tuple<string, int, List<string>>(
            "Arina Biswas"
            , 26
    , new List<string> { "Mathematics","English","Accountancy","Finance"}));
          

    In the above code segment, we have instantiated a collection of Tuple<string, int, List<string>> . The instantiated object is represented by studentTupleInstances. Next we have added three items to the generic collection by using the Add method.

  2. By using the static Create method of Tuple Class

    E.g.

    List<Tuple<string, int, List<string>>> studentTupleStaticCreates = new List<Tuple<string, int, List<string>>>();
    
        Enumerable
            .Range(1, 3)
            .ToList()
            .ForEach
            (
                    i => studentTupleStaticCreates.Add(Tuple.Create(
                         string.Concat("Student", i),
                         i + 16,
                         new List<string>()
                         {
                          string.Concat("Subject", i),
                          string.Concat("Subject", i+1),
                          string.Concat("Subject", i+2)
                         })
           ));
            

    In the above example, first by using Enumerable.Range(1, 3) we are creating a range of sequential numbers upto 3 from 1 and then adding some records dynamically by using the Tuple.Create static method to the studentTupleStaticCreates collection.

    The representation of the same will look as under at runtime

    We can access the generic list as describe under

    1. By using Foreach Loop

      foreach(var student in studentTupleInstances)
                      {
                         Console.WriteLine("Student Name :  " + student.Item1);
                         Console.WriteLine("Student Age :  " + student.Item2);
      
                         foreach(var subject in student.Item3)
                         {
                             Console.WriteLine("Is reading : " + subject);
                         }
      
                         Console.WriteLine("------------------------------------------");
                      }
                      
    2. By using Statement Lambda

      studentTupleInstances
                      .ForEach
                      (student =>
      
                            {
                              Console.WriteLine("Student Name :  " + student.Item1);
                             Console.WriteLine("Student Age :  " + student.Item2);
      
                             student.Item3
                             .ForEach
                             (
                                subject=> Console.WriteLine("Is reading : " + subject)
                             );
      
                             Console.WriteLine("------------------------------------------");
                           }
                      );
                      

    In both the cases, we will get the below result

Conclusion

In the introductory article with Tuple, we learnt about What a Tuple is Why we need Tuple, When to use it, How to create Tuple and How to access the same in C#. Hope this will be helpful. This this article we learnt about Creation of Generic Collections and Accessing the same using Tuple. More to come in the following series. Zipped file attached.

Recommendation
Read Let us Learn Tuple-Part3 ( Tuple and Arrays) 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,Microsoft_MVP,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)