Need of a tuple in C sharp
Imagine a scenario where you need to store the details of a person by using a single entity like the name, id and blood group, etc. The common and the easiest way of declaring these items in a small field is a data structure that comes to mind is a tuple. In comparison with arrays, these can store items of a specific data type and can store infinite items, whereas tuples store items up to a limited value.
We will use an example to demonstrate the creation of a tuple through the tuple class. Usually, we need to identify the type of each item that is to be added to a tuple; this requires a lot of time consumption and also creates the code messy. This can be overcome through a tuple class. This class has methods that are static by nature; by using them, we do not need to describe the data type of the object. We just keep on adding items up to the limit of 8.
Coming towards an example, we have quoted here to elaborate on the creation of a tuple in C sharp. To start the source code, use the library that contains a description regarding each class, array, and other containers like tuples as well.
Create a class and inside it, declare the main program. A create method is used for the creation of a tuple. An object is created through a built-in create function present inside the tuple class. This function as a parameter contains the value you want to enter in the tuple.
Here My_tuple1 is an object. This was an example of a single tuple. We only define a string. This string was identified by the object; that’s why we did not need to declare the variable type for the value.
Similarly, by following the same approach, we can create four tuples through the object. This time all the values are in integer data type.
We can create several tuples depending on our own choice. Till now, we created tuples of a single data type, either a string or an integer. Now we will create a tuple of mixed data types.
This example of creating a tuple contains almost all data types, including an integer, a string, and a double value. So we don’t need to mention any data type.
When a tuple is created, a part of the memory slot is allotted to the values inside it. After creation, these tuples need to get accessed. All the elements inside the tuple are accessed by using an item property, in which we declare the element number inside the tuple; as in the array, we used to mention the index number, so we mention the item number in the case of tuples. So an item is accessible through the object using the element number. We will elaborate on this in the below-given source code.
So first, we will create a sample tuple by providing a value; the tuple is named My_tuple1. Now we will access this tuple with the help of the console.writeline command, as a result, will be displayed on the terminal.
You can see that as there was a single item, we used it through the tuple object.
Similarly, 4 tuples are created as we did in the previous example. These will be displayed by using the item numbers according to their existence. In each console line, a single item is used only at a time. So you can see that to display all the four items in the tuple, we will display them separately.
After using four, now 8 tuples are created likewise. And similarly, each item is accessed through the item number along with the tuple. When more than tuples are used in a single program, then a specific item is accessed by mentioning the tuple name object.
Save this code. We will execute the program in a Linux terminal with the use of the MCS compiler and mono to execute the code.
$ mono file.exe
All the elements are accessed through the item number property through the tuple objects of respective tuples.
In C sharp, you are allowed to add a maximum of 8 items in a single tuple. This becomes a limited situation. So to create a tuple and have items of the same type, we use a nested tuple as the name shows that this deals with a tuple inside another tuple. A nested tuple can be created anywhere in the program, but by following the recommendations, we always declare it at the end. All the eight items will be accessed through the item property, whereas the nested tuple items will be accessed through the rest property.
So first, we will create a nested tuple.
You can see that a simple tuple contains 6 items on its own, and at 7th, a nested tuple is declared, which further contains 5 items.
Now we will access the items of the tuple using the item property.
All the 7 items are accessed through the item property, and then we will use the rest property for the nested tuple to display all items collectively.
Whereas if you want to display the items of the nested tuple, then this is done by item property.
It will be accessed by using the rest keyword and then the tuple name, with the tuple item inside the nested tuple. \
On execution, you can see that all the items are displayed through the item and the rest property.
A tuple is a data structure that is used to store details of more than one attribute of a single person in one entity. A tuple can be created through a constructor and through the tuple object, whose properties are present inside the tuple class. But the preferable method is to use the object method. Similar to creation, there are two ways to access tuples. One method is through the item property, and the other approach is to use the rest property. We have used both approaches in examples to elaborate on the accessing ability in tuples, especially when we introduce nested tuples in our program.