Open In App

How to create 7-ValueTuple in C#?

In C#, a 7-ValueTuple is a value type tuple which contains seven elements and it is also known as septuple. You can create a 7-ValueTuple using two different ways:

  1. Using ValueTuple <T1, T2, T3, T4, T5, T6, T7>(T1, T2, T3, T4, T5, T6, T7) Constructor
  2. Using Create <T1, T2, T3, T4, T5, T6, T7>(T1, T2, T3, T4, T5, T6, T7) Method

Using ValueTuple <T1, T2, T3, T4, T5, T6, T7>(T1, T2, T3, T4, T5, T6, T7) Constructor

You can create a 7-ValueTuple by using ValueTuple <T1, T2, T3, T4, T5, T6, T7>(T1, T2, T3, T4, T5, T6, T7) constructor. It initializes a new instance of the ValueTuple <T1, T2, T3, T4, T5, T6, T7> 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.



Syntax:

public ValueTuple (T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7);



Parameters:

Example:




// C# program to create a 7-ValueTuple
// using value tuple constructor
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating a value tuple with seven elements
        // Using ValueTuple<T1, T2, T3, T4, T5, T6,
        // T7>(T1, T2, T3, T4, T5, T6, T7) constructor
        ValueTuple<string, string, string, string, string, string,
            string> MyTpl = new ValueTuple<string, string, string
              string, string, string, string>("Dog", "Cat", "Cow",
                                    "Pig", "Hen", "Bird", "Fish");
  
        Console.WriteLine("Component 1: " + MyTpl.Item1);
        Console.WriteLine("Component 2: " + MyTpl.Item2);
        Console.WriteLine("Component 3: " + MyTpl.Item3);
        Console.WriteLine("Component 4: " + MyTpl.Item4);
        Console.WriteLine("Component 5: " + MyTpl.Item5);
        Console.WriteLine("Component 6: " + MyTpl.Item6);
        Console.WriteLine("Component 7: " + MyTpl.Item7);
    }
}

Output:
Component 1: Dog
Component 2: Cat
Component 3: Cow
Component 4: Pig
Component 5: Hen
Component 6: Bird
Component 7: Fish

Using Create <T1, T2, T3, T4, T5, T6, T7>(T1, T2, T3, T4, T5, T6, T7) Method

You can also create a 7-ValueTuple or a value tuple which holds 7-elements with the help of Create <T1, T2, T3, T4, T5, T6, T7>(T1, T2, T3, T4, T5, T6, T7) method. When you use this method, then there is no need to specify the type of the elements stored in the value tuple.

Syntax:

public static ValueTuple<T1, T2, T3, T4, T5, T6, T7> Create<T1, T2, T3, T4, T5, T6, T7> (T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7);

Type Parameters:

Parameters:

Returns: This method returns a value tuple with seven elements.

Example:




// C# program to create a 7-ValueTuple
// using Create<T1, T2, T3, T4, T5, T6,
// T7>(T1, T2, T3, T4, T5, T6, T7) Method
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating a value tuple with seven elements
        // Using Create<T1, T2, T3, T4, T5, T6, 
        // T7>(T1, T2, T3, T4, T5, T6, T7) method
        var MyTple = ValueTuple.Create(12, 34, 56, 45,
                                          67, 89, 78);
  
        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);
    }
}

Output:
Component 1: 12
Component 2: 34
Component 3: 56
Component 4: 45
Component 5: 67
Component 6: 89
Component 7: 78

Reference:


Article Tags :
C#