How to create 8-Tuple or Octuple in C#?
In C#, an 8-tuple is a tuple that contains eight elements and it is also known as Octuple. You can create an 8-tuple using two different ways:
- Using Tuple<T1,T2,T3,T4,T5,T6,T7,TRest>(T1, T2, T3, T4, T5, T6, T7, TRest) Constructor
- Using the Create method
You can create 8-tuple using Tuple < T1, T2, T3, T4, T5, T6, T7, TRest>(T1, T2, T3, T4, T5, T6, T7, TRest) constructor. It initializes a new instance of the Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> class. But when you create a tuple using this constructor then you have to specify the type of the element stored in the tuple.
Note: If you will this constructor, then you can also create a tuple that contains eight or more than eight elements by using the rest parameter to create n-nested tuples and each tuple contains from one to seven components.
Syntax:
public Tuple (T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7, TRest rest);
Parameters:
- item1: It is the value of the first tuple component.
- item2: It is the value of the second tuple component.
- item3: It is the value of the third tuple component.
- item4: It is the value of the fourth tuple component.
- item5: It is the value of the fifth tuple component.
- item6: It is the value of the sixth tuple component.
- item7: It is the value of the seventh tuple component.
- rest: It is any generic Tuple object which contains the values of the tuple’s remaining components.
Exception: This will give ArgumentException if the rest is not a generic Tuple object.
Example:
// C# program to create 8-tuple // using the tuple constructor using System; public class GFG { // Main method static public void Main() { // Creating tuple with eight elements // Using Tuple<T1, T2, T3, T4, T5, T6, // T7, TRest>(T1, T2, T3, T4, T5, T6, // T7, TRest) constructor Tuple< int , int , int , int , int , int , int , Tuple< int >> My_Tuple = new Tuple< int , int , int , int , int , int , int , Tuple< int > >(22, 33, 44, 545, 55, 88, 66, new Tuple< int >(77)); 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); Console.WriteLine( "Element 8: " + +My_Tuple.Rest.Item1); } } |
Element 1: 22 Element 2: 33 Element 3: 44 Element 4: 545 Element 5: 55 Element 6: 88 Element 7: 66 Element 8: 77
You can also create 8-tuple with the help of Create method. When you use this method then there is no need to specify the type of the elements stored in the tuple.
Syntax:
public static Tuple < T1, T2, T3, T4, T5, T6, T7, Tuple < T8>> Create < T1, T2, T3, T4, T5, T6, T7, T8> (T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7, T8 item8);
Type Parameters:
- T1: It is the type of the first tuple component.
- T2: It is the type of the second tuple component.
- T3: It is the type of the third tuple component.
- T4: It is the type of the fourth tuple component.
- T5: It is the type of the fifth tuple component.
- T6: It is the type of the sixth tuple component.
- T7: It is the type of the seventh tuple component.
- T8: It is the type of the eighth tuple component.
Parameters:
- item1: It is the value of the first tuple component.
- item2: It is the value of the second tuple component.
- item3: It is the value of the third tuple component.
- item4: It is the value of the fourth tuple component.
- item5: It is the value of the fifth tuple component.
- item6: It is the value of the sixth tuple component.
- item7: It is the value of the seventh tuple component.
- item8: It is the value of the eighth tuple component.
Return Type: This method returns 8-tuple whose value is item1, item2, item3, item4, item5, item6, item7, and item8.
Example:
// C# program to create 8-tuple // using create method using System; public class GFG { // Main method static public void Main() { // Creating tuple with eight // elements Using Create method var My_Tuple = Tuple.Create( "C" , "C++" , "Ruby" , "Java" , "Perl" , "PHP" , "Python" , "Scala" ); 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); Console.WriteLine( "Element 8: " + My_Tuple.Rest); } } |
Element 1: C Element 2: C++ Element 3: Ruby Element 4: Java Element 5: Perl Element 6: PHP Element 7: Python Element 8: (Scala)
Reference:
- https://docs.microsoft.com/en-us/dotnet/api/system.tuple-8.-ctor?view=netframework-4.8
- https://docs.microsoft.com/en-us/dotnet/api/system.tuple.create?view=netframework-4.8#System_Tuple_Create__8___0___1___2___3___4___5___6___7_
Contact Us