Skip to content
Related Articles

Related Articles

C# | Tuple<T1,T2,T3,T4,T5,T6,T7> Class
  • Last Updated : 02 Dec, 2019

The Tuple<T1, T2, T3, T4, T5, T6, T7> class is used to create a 7-tuple or septuple. It represents a tuple which contains the seven elements in it. You can instantiate a Tuple<T1, T2, T3, T4, T5, T6, T7> object by calling either the Tuple<T1, T2, T3, T4, T5, T6, T7>(T1, T2, T3, T4, T5, T6, T7) constructor or by the static Tuple.Create method. You can retrieve the value of the tuple’s elements by using the read-only Item1, Item2, Item3, Item4, Item5, Item6, and Item7 instance property.

Important Points:

  • It implements IStructuralComparable, IStructuralEquatable, and IComparable interface.
  • It defined under System namespace.
  • It represent multiple data into a single data set.
  • It allows us to create, manipulate, and access data set.
  • It returns multiple values from a method without using out parameter.
  • It allows passing multiple values to a method with the help of single parameters.
  • It can also store duplicate elements.

Constructor

ConstructorDescription
Tuple<T1, T2, T3, T4, T5, T6, T7>(T1, T2, T3, T4, T5, T6, T7)Initializes a new instance of the Tuple<T1, T2, T3, T4, T5, T6, T7> class.

Property

PropertyDescription
Item1Gets the value of the Tuple<T1, T2, T3, T4, T5, T6, T7> object’s first component.
Item2Gets the value of the current Tuple<T1, T2, T3, T4, T5, T6, T7> object’s second component.
Item3Gets the value of the current Tuple<T1, T2, T3, T4, T5, T6, T7> object’s third component.
Item4Gets the value of the current Tuple<T1, T2, T3, T4, T5, T6, T7> object’s fourth component.
Item5Gets the value of the current Tuple<T1, T2, T3, T4, T5, T6, T7> object’s fifth component.
Item6Gets the value of the current Tuple<T1, T2, T3, T4, T5, T6, T7> object’s sixth component.
Item7Gets the value of the current Tuple<T1, T2, T3, T4, T5, T6, T7> object’s seventh component.

Example:




// C# progtam to illustrate the constructor
// and property of Tuple<T1, T2, T3, T4, 
// T5, T6, T7> Class
using System;
   
class GFG {
  
    // Main Method
    static public void Main () {
  
        // Creating 7-Tuple
        // Using Tuple<T1, T2, T3, T4, T5, T6,
        // T7>(T1, T2, T3, T4, T5, T6, T7) constructor
        Tuple<int, int, int, string, int, string, int>mytuple = new Tuple<int,
                      int, int, string, int, string, int>(79, 34, 67, "Geeks"
                                                     44, "GeeksforGeeks", 66);
           
        // Accessing the values
        Console.WriteLine("Value of the First Component: " + mytuple.Item1);
        Console.WriteLine("Value of the Second Component: " + mytuple.Item2);
        Console.WriteLine("Value of the Third Component: " + mytuple.Item3);
        Console.WriteLine("Value of the Fourth Component: " + mytuple.Item4);
        Console.WriteLine("Value of the Fifth Component: " + mytuple.Item5);
        Console.WriteLine("Value of the Sixth Component: " + mytuple.Item6);
        Console.WriteLine("Value of the Seventh Component: " + mytuple.Item7);
  
    }
}
Output:
Value of the First Component: 79
Value of the Second Component: 34
Value of the Third Component: 67
Value of the Fourth Component: Geeks
Value of the Fifth Component: 44
Value of the Sixth Component: GeeksforGeeks
Value of the Seventh Component: 66

Methods

MethodDescription
Equals(Object)Returns a value that indicates whether the current Tuple<T1, T2, T3, T4, T5, T6, T7> object is equal to a specified object.
GetHashCode()Returns the hash code for the current Tuple<T1, T2, T3, T4, T5, T6, T7> object.
GetType()Gets the Type of the current instance.
MemberwiseClone()Creates a shallow copy of the current Object.
ToString()Returns a string that represents the value of this Tuple<T1, T2, T3, T4, T5, T6, T7> instance.

Example:






// C# program to check whether the 
// given tuples are equal or not
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating 7-Tuple
        // 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> mytuple1 = new Tuple<int
                int, int, int, int, int, int>(20, 40, 90, 89, 33, 66, 87);
  
        Tuple<int, int, int, int, int, int, int> mytuple2 = new Tuple<int, int
                          int, int, int, int, int>(20, 40, 90, 89, 33, 66, 87);
  
        // Using Equals method
        if (mytuple1.Equals(mytuple2)) 
        {
            Console.WriteLine("Tuple Matched.");
        }
  
        else 
        {
            Console.WriteLine("Tuple Not Matched.");
        }
    }
}
Output:
Tuple Matched.

Reference:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :