How to create 8-ValueTuple in C#?
In C#, an 8-ValueTuple is a value type tuple which contains eight elements and it is also known as octuple. You can create an 8-ValueTuple using two different ways:
- Using ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest<(T1, T2, T3, T4, T5, T6, T7, TRest) Constructor
- Using Create<T1, T2, T3, T4, T5, T6, T7, T8<(T1, T2, T3, T4, T5, T6, T7, T8) Method
Using ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest<(T1, T2, T3, T4, T5, T6, T7, TRest) Constructor
You can create an 8-ValueTuple by using ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest<(T1, T2, T3, T4, T5, T6, T7, TRest) constructor. It initializes a new instance of the ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest< struct. But when you create a value tuple using this constructor, then you have to specify the type of the element stored in the value tuple. Using this constructor, you can also create a value tuple which contains nine or more than nine elements by using the rest parameter. With the help of the rest parameter, you can create n-nested value tuples.
Syntax:
public ValueTuple (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 value tuple component.
- item2: It is the value of the second value tuple component.
- item3: It is the value of the third value tuple component.
- item4: It is the value of the fourth value tuple component.
- item5: It is the value of the fifth value tuple component.
- item6: It is the value of the sixth value tuple component.
- item7: It is the value of the seventh value tuple component.
- rest: It is the instance of any value tuple type that contains the values of the value’s tuple’s remaining elements.
Exception: This will give ArgumentException if the rest is not a generic value tuple type.
Example:
// C# program to create a 8-ValueTuple // using ValueTuple constructor using System; class GFG { // Main method static public void Main() { // Creating a value tuple with eight elements // Using ValueTuple<T1, T2, T3, T4, T5, T6, // T7, TRest>(T1, T2, T3, T4, T5, T6, T7, // TRest rest) constructor ValueTuple<string, string, string, string, string, string, string, ValueTuple<string>> MyTpl = new ValueTuple<string, string, string, string, string, string, string, ValueTuple<string>>("Rohan", "Sumit", "Soniya", "Shivam", "Raksha", "Purvi", "Mohan", new ValueTuple<string>("Nayan")); Console.WriteLine("Student 1: " + MyTpl.Item1); Console.WriteLine("Student 2: " + MyTpl.Item2); Console.WriteLine("Student 3: " + MyTpl.Item3); Console.WriteLine("Student 4: " + MyTpl.Item4); Console.WriteLine("Student 5: " + MyTpl.Item5); Console.WriteLine("Student 6: " + MyTpl.Item6); Console.WriteLine("Student 7: " + MyTpl.Item7); Console.WriteLine("Student 8: " + MyTpl.Rest); } } |
Student 1: Rohan Student 2: Sumit Student 3: Soniya Student 4: Shivam Student 5: Raksha Student 6: Purvi Student 7: Mohan Student 8: (Nayan)
Using Create<T1, T2, T3, T4, T5, T6, T7, T8<(T1, T2, T3, T4, T5, T6, T7, T8) Method
You can also create an 8-ValueTuple or a value tuple which holds 8-elements with the help of Create<T1, T2, T3, T4, T5, T6, T7, T8<(T1, T2, T3, T4, T5, T6, T7, T8) method. When you use this method, then there is no need to specify the type of elements stored in the value tuple.
Syntax:
public static ValueTuple <T1, T2, T3, T4, T5, T6, T7, ValueTuple<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 value tuple’s first component.
- T2: It is the type of the value tuple’s second component.
- T3: It is the type of the value tuple’s third component.
- T4: It is the type of the value tuple’s fourth component.
- T5: It is the type of the value tuple’s fifth component.
- T6: It is the type of the value tuple’s sixth component.
- T7: It is the type of the value tuple’s seventh component.
- T8: It is the type of the value tuple’s eighth component.
Parameters:
- item1: It is the value of the value tuple’s first component.
- item2: It is the value of the value tuple’s second component.
- item3: It is the value of the value tuple’s third component.
- item4: It is the value of the value tuple’s fourth component.
- item5: It is the value of the value tuple’s fifth component.
- item6: It is the value of the value tuple’s sixth component.
- item7: It is the value of the value tuple’s seventh component.
- item8: It is the value of the value tuple’s eighth component.
Returns: This method returns a value tuple with eight elements.
Example:
// C# program to create a 8-ValueTuple // using Create Method using System; class GFG { // Main method static public void Main() { // Creating a value tuple with the // eight elements using Create method var MyTple = ValueTuple.Create(45.67, 67.78, 56.8, 567.5, 23.4, 56.7, 12.34, 35.56); // Display the components of the value tuple Console.WriteLine("Component 1: " + MyTple.Item1); Console.WriteLine("Component 2: " + MyTple.Item2); Console.WriteLine("Component 3: " + MyTple.Item3); Console.WriteLine("Component 4: " + MyTple.Item4); Console.WriteLine("Component 5: " + MyTple.Item5); Console.WriteLine("Component 6: " + MyTple.Item6); Console.WriteLine("Component 7: " + MyTple.Item7); Console.WriteLine("Component 8: " + MyTple.Item7); } } |
Component 1: 45.67 Component 2: 67.78 Component 3: 56.8 Component 4: 567.5 Component 5: 23.4 Component 6: 56.7 Component 7: 12.34 Component 8: 12.34
Reference:
- https://docs.microsoft.com/en-us/dotnet/api/system.valuetuple-8.-ctor?view=netframework-4.8
- https://docs.microsoft.com/en-us/dotnet/api/system.valuetuple.create?view=netframework-4.8#System_ValueTuple_Create__8___0___1___2___3___4___5___6___7_
Recommended Posts:
- C# | How to create a SortedList
- How to create a StringDictionary in C#
- How to create 1-ValueTuple in C#?
- How to create 2-ValueTuple in C#?
- How to create an OrderedDictionary in C#
- How to Create a Range to a Specified End in C#?
- How to create a StringCollection in C#
- How to create a ListDictionary in C#
- How to create the StringBuilder in C#
- C# | How to create a SortedSet
- How to create 6-Tuple in C#?
- How to create a Queue in C#
- How to create Threads in C#
- How to create 6-ValueTuple in C#?
- How to create 7-ValueTuple in C#?
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.


