c sharp

C# tuple

A tuple is said to be a type of data structure that has multiple parts in it. It shows a set of data that contains multiple values that may or may not be related to one another; they may be of different data types. Tuples are usually designed to store items of up to 8 elements, so if you use more than 8 items, an error will occur. The basic feature of the tuple is that it allows us to add multiple types of data in a single set. It returns multiple values from the method that is done without using parameters. Many duplicate items can be stored inside the tuple.

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.

Example 1

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.

Using system;

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.

# Var My_Tuple1 = Tuple.Create("Linuxhint");

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.

# Var My_tupe2 = Tuple.Create(12, 30, 40, 50);

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.

# Var My_Tuple3 = Tuple. Create (13, "Linux", 67, 89.90, 'g', 39939, "Hint", 10);

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.

Example 2

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.

# Console.writeline ("Elements of My_Tuple1: " + My_Tuple1.Item1);

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.

# Console.WriteLine ("Element of My_Tuple2: " +My_Tuple2.Item1);

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.

$ MCS file.c
$ mono file.exe

All the elements are accessed through the item number property through the tuple objects of respective tuples.

Example 3

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.

# var My_Tuple = Tuple. Create (13, "Linux", 67, 89.90, 'L', 39939, "hint", Tuple. Create (12, 30, 40, 50));

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.

# Console.writeline ("Element of My_Tuple: " +My_Tuple.Item1);

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.

# Console.writeline("Elements of nested tuple: " +My_tuple.Rest);

Whereas if you want to display the items of the nested tuple, then this is done by item property.

# My_Tuple. Rest. Item1.Item1;

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.

Conclusion

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.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.