In C#, Tuple class is used to provide static methods for creating tuples and this class defined under System namespace. This class itself does not represent a tuple, but it provides static methods that are used to create an instance of the tuple type. Or in other words, the Tuple class provides helper methods that are used to instantiate tuple objects without having to explicitly specify the type of each tuple component. In tuple, you can only store elements from one to eight, if you try to store elements greater than eight without nested tuple, then the compiler will give an error.
Generally, tuples are used:
- To represent multiple data into a single data set.
- To create, manipulate, and access data set.
- To return multiple values from a method without using out parameters.
- To pass multiple values to a method with the help of single parameters.
Note: You can also, create a tuple with the help of the constructors which are provided by tuple classes, but in constructors, you have to specify the type of the elements present in the tuple as shown in the below example:
Example:
// C# program to create tuple // using tuple constructor. using System; class GFG { // Main method static public void Main() { // Creating tuple with seven elements // Using Tuple<T1, T2, T3, T4, T5, T6, // T7>(T1, T2, T3, T4, T5, T6, T7) constructor Tuple<int, int, int, int, int, int, int> My_Tuple = new Tuple<int, int, int, int, int, int, int>(22, 334, 54, 65, 76, 87, 98); Console.WriteLine("Element 1: " + My_Tuple.Item1); Console.WriteLine("Element 2: " + My_Tuple.Item2); Console.WriteLine("Element 3: " + My_Tuple.Item3); Console.WriteLine("Element 4: " + My_Tuple.Item4); Console.WriteLine("Element 5: " + My_Tuple.Item5); Console.WriteLine("Element 6: " + My_Tuple.Item6); Console.WriteLine("Element 7: " + My_Tuple.Item7); } } |
Element 1: 22 Element 2: 334 Element 3: 54 Element 4: 65 Element 5: 76 Element 6: 87 Element 7: 98
Methods
| Method | Description |
|---|---|
| Create <T1>(T1) | Creates a new 1-tuple, or singleton. |
| Create <T1, T2>(T1, T2) | Creates a new 2-tuple, or pair. |
| Create <T1, T2, T3>(T1, T2, T3) | Creates a new 3-tuple, or triple. |
| Create <T1, T2, T3, T4>(T1, T2, T3, T4) | Creates a new 4-tuple, or quadruple. |
| Create <T1, T2, T3, T4, T5>(T1, T2, T3, T4, T5) | Creates a new 5-tuple, or quintuple. |
| Create <T1, T2, T3, T4, T5, T6>(T1, T2, T3, T4, T5, T6) | Creates a new 6-tuple, or sextuple. |
| Create <T1, T2, T3, T4, T5, T6, T7>(T1, T2, T3, T4, T5, T6, T7) | Creates a new 7-tuple, or septuple. |
| Create <T1, T2, T3, T4, T5, T6, T7, TRest>(T1, T2, T3, T4, T5, T6, T7, T8) | Creates a new 8-tuple, or octuple. |
Example:
// C# program to create 3-tuple // using create method using System; class GFG { // Main method static public void Main() { // Creating tuple with three elements // Using Create method var My_Tuple = Tuple.Create("Geeks", 2323, 'g'); Console.WriteLine("Element 1: " + My_Tuple.Item1); Console.WriteLine("Element 2: " + My_Tuple.Item2); Console.WriteLine("Element 3: " + My_Tuple.Item3); } } |
Element 1: Geeks Element 2: 2323 Element 3: g
Reference:
Recommended Posts:
- How to create 3-Tuple or Triple Tuple in C#?
- How to create 1-Tuple or Singleton Tuple in C#?
- How to create 2-tuple or pair tuple in C#?
- C# | Tuple<T1> Class
- C# | Tuple<T1,T2> Class
- C# | Tuple<T1,T2,T3> Class
- C# | Tuple<T1,T2,T3,T4> Class
- C# | Tuple<T1,T2,T3,T4,T5> Class
- C# | Tuple<T1,T2,T3,T4,T5,T6> Class
- C# | Tuple<T1,T2,T3,T4,T5,T6,T7> Class
- C# | Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> Class
- C# | How to get the HashCode of the tuple?
- C# | How to get the Remaining Elements of the Tuple?
- C# | Tuple
- How to create 4-Tuple or quadruple in C#?
- How to create 5-Tuple or quintuple in C#?
- How to create 6-Tuple in C#?
- How to create 7-Tuple or Septuple in C#?
- How to create 8-Tuple or Octuple in C#?
- C# | Getting the Type of the Tuple's Element
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

