Understanding C# Tuple T1, T2, T3, T4, T5 Class

The Tuple <T1, T2, T3, T4, T5> classes are used to create a 5-tuple or 5-tuple。 It represents a tuple containing five elements. You can instantiate the Tuple <T1, T2, T3, T4, T5> object tuple <T1, T2, T3, T4, T5> (T1, T2, T3, T4, T5) constructor or create a method from a static tuple. You can use read-only to retrieve the values of tuple elements for item 1, item 2, item 3, item 4 and item 5 instance attributes.

Important Notes:

  • It implements structurally comparable, structurally equal, and comparable interfaces.
  • It is defined under the system namespace.
  • It means merging multiple data into a single data set.
  • It allows us to create, manipulate and access datasets.
  • It returns multiple values from a single method without using the out parameter.
  • It allows multiple values to be passed to a method with the help of a single parameter.
  • It can also store duplicate elements.

Table of Contents

Builder

Tuple<T1, T2, T3, T4, T5>(T1, T2, T3, T4, T5)Initialize a new instance of Tuple <T1, T2, T3, T4, T5 > classes.

Attribute

Item 1Gets the value of the first component of the Tuple <T1, T2, T3, T4, T5 > object.
Item 2Gets the value of the second component of the current Tuple <T1, T2, T3, T4, T5 > object.
ITEM 3Gets the value of the third component of the current Tuple <T1, T2, T3, T4, T5 > object.
Item 4Gets the value of the fourth part of the current Tuple <T1, T2, T3, T4, T5 > object.
ITEM 5Gets the value of the fifth component of the current Tuple <T1, T2, T3, T4, T5 > object.

Example:

//C# program to illustrate the constructor
//and property of Tuple<T1, T2, T3, T4, T5> Class
using System;
   
class GFG {
  
     //Main Method
     static public void Main () {
  
         //Creating 5-Tuple
         //Using Tuple<T1, T2, T3, T4, //T5>(T1, T2, T3, T4, T5) constructor
         Tuple<int , int , int , string , int>mytuple = new Tuple<int , int , int , string , int>(79, 34, 67, "Geeks" , 44);
           
         //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);
  
     }
}

The output is as follows:

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

Method

Equals (Object)Returns a value that indicates whether the current Tuple <T1, T2, T3, T4, T5> object is equal to the specified object.
GetHashCode()Returns the hash code of the current Tuple <T1, T2, T3, T4, T5 > object.
GetType()Obtain the type of the current instance.
MemberwiseClone()Creates a superficial copy of the current object.
ToString()Returns a string representing the value of this Tuple <T1, T2, T3, T4, T5 > 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 5-Tuple
         //Using Tuple<T1, T2, T3, T4, //T5>(T1, T2, T3, T4, T5) constructor
         Tuple<int , int , int , int , int> mytuple1 = new Tuple<int , int , int , int , int>(20, 40, 90, 89, 33);
  
         Tuple<int , int , int , int , int> mytuple2 = new Tuple<int , int , int , int , int>(20, 40, 90, 89, 33);
  
         //Using Equals method
         if (mytuple1.Equals(mytuple2)) 
         {
             Console.WriteLine( "Tuple Matched.." );
         }
  
         else 
         {
             Console.WriteLine( "Tuple not Matched.." );
         }
     }
}

The output is as follows:

Tuple Matched..

Reference:

  • https://docs.microsoft.com/en-us/dotnet/api/system.tuple-5?view=netframework-4.8