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
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.
-
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.
-
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
-
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("------------------------------------------");
}
-
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.