Open In App

C# | Tuple<T1,T2,T3> Class

Last Updated : 23 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The Tuple<T1, T2, T3> class is used to create a 3-tuple or triple. It represents a tuple which contains the three elements in it. You can instantiate a Tuple<T1, T2, T3> object by calling either the Tuple<T1, T2, T3>(T1, T2, T3) 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, and Item3 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

.tuple-class-table { border-collapse: collapse; width: 100%; } .tuple-class-table td { border: 1px solid #5fb962; text-align: left !important; padding: 8px; } .tuple-class-table th { border: 1px solid #5fb962; padding: 8px; } .tuple-class-table tr>th{ background-color: #c6ebd9; vertical-align: middle; } .tuple-class-table tr:nth-child(odd) { background-color: #ffffff; }  

Constructor Description
Tuple<T1, T2, T3>(T1, T2, T3) Initializes a new instance of the Tuple<T1, T2, T3> class.

Property

Property Description
Item1 Gets the value of the Tuple<T1, T2, T3> object’s first component.
Item2 Gets the value of the current Tuple<T1, T2, T3> object’s second component.
Item3 Gets the value of the current Tuple<T1, T2, T3> object’s third component.

Example:

csharp




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


Output: 

Value of the First Component: 79
Value of the Second Component: 34
Value of the Third Component: 67

 

Methods

Method Description
Equals(Object) Returns a value that indicates whether the current Tuple<T1, T2, T3> object is equal to a specified object.
GetHashCode() Returns the hash code for the current Tuple<T1, T2, T3> 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> instance.

Example:

CSharp




// C# program to check whether the
// given tuples are equal or not
using System;
 
class GFG {
 
    // Main method
    static public void Main()
    {
 
        // Creating 3-Tuple
        // Using Tuple<T1, T2, T3>(T1, T2, T3) constructor
        Tuple<int, int, int> mytuple1 = new Tuple<int, int, int>(20, 40, 90);
        Tuple<int, int, int> mytuple2 = new Tuple<int, int, int>(20, 49, 87);
 
        // Using Equals method
        if (mytuple1.Equals(mytuple2))
        {
            Console.WriteLine("Tuple Matched..");
        }
 
        else
        {
            Console.WriteLine("Tuple not matched..");
        }
    }
}


Output: 

Tuple not matched..

 

Reference: 



Similar Reads

How to create 3-Tuple or Triple Tuple in C#?
In C#, a 3-tuple is a tuple that contains three elements and it is also known as triple. You can create a 3-tuple using two different ways: Using Tuple&lt;T1,T2,T3&gt;(T1, T2, T3) Constructor Using the Create method Using Tuple&lt;T1,T2,T3&gt;(T1, T2, T3) Constructor You can create 3-tuple using Tuple&lt;T1, T2,T3&gt;(T1, T2, T3) constructor. It in
2 min read
How to create 1-Tuple or Singleton Tuple in C#?
In C#, a singleton tuple is a tuple that contains only one element and singleton tuple is also known as 1-tuple. You can create a 1-tuple using two different ways: Using Tuple&lt;T1&gt;(T1) Constructor Using the Create method Using Tuple&lt;T1&gt;(T1) Constructor You can create a singleton tuple using Tuple &lt;T1&gt;(T1) constructor. It initialize
2 min read
How to create 2-tuple or pair tuple in C#?
In C#, a 2-tuple is a tuple that contains two elements and it is also known as pair. You can create a 2-tuple using two different ways: Using Tuple&lt;T1,T2&gt;(T1, T2) Constructor Using the Create method Using Tuple&lt;T1,T2&gt;(T1, T2) Constructor You can create 2-tuple using Tuple&lt;T1, T2&gt;(T1, T2) constructor. It initializes a new instance
2 min read
C# | Tuple&lt;T1&gt; Class
The Tuple&lt;T1&gt; class is used to create 1-tuple or singleton which contains only a single element in it. You can instantiate a Tuple &lt;T1&gt; object by calling either the Tuple&lt;T1&gt; constructor or by the static Tuple.Create method. You can retrieve the value of the tuple's single element by using the read-only Item1 instance property. Im
2 min read
C# | Tuple&lt;T1,T2&gt; Class
The Tuple&lt;T1, T2&gt; class is used to create a 2-tuple or pair. It represents a tuple which contains the two elements in it. You can instantiate a Tuple&lt;T1, T2&gt; object by calling either the Tuple&lt;T1, T2&gt;(T1, T2) constructor or by the static Tuple.Create method. You can retrieve the value of the tuple's elements by using the read-only
3 min read
C# | Tuple&lt;T1,T2,T3,T4&gt; Class
The Tuple&lt;T1, T2, T3, T4&gt; class is used to create a 4-tuple or quadruple. It represents a tuple which contains the four elements in it. You can instantiate a Tuple&lt;T1, T2, T3, T4&gt; object by calling either the Tuple&lt;T1, T2, T3, T4&gt;(T1, T2, T3, T4) constructor or by the static Tuple.Create method. You can retrieve the value of the t
3 min read
C# | Tuple&lt;T1,T2,T3,T4,T5&gt; Class
The Tuple&lt;T1, T2, T3, T4, T5&gt; class is used to create a 5-tuple or quintuple. It represents a tuple which contains the five elements in it. You can instantiate a Tuple&lt;T1, T2, T3, T4, T5&gt; object by calling either the Tuple&lt;T1, T2, T3, T4, T5&gt;(T1, T2, T3, T4, T5) constructor or by the static Tuple.Create method. You can retrieve th
3 min read
C# | Tuple&lt;T1,T2,T3,T4,T5,T6&gt; Class
The Tuple&lt;T1, T2, T3, T4, T5, T6&gt; class is used to create a 6-tuple or sextuple. It represents a tuple which contains the six elements in it. You can instantiate a Tuple&lt;T1, T2, T3, T4, T5, T6&gt; object by calling either the Tuple&lt;T1, T2, T3, T4, T5, T6&gt;(T1, T2, T3, T4, T5, T6) constructor or by the static Tuple.Create method. You c
4 min read
C# | Tuple&lt;T1,T2,T3,T4,T5,T6,T7,TRest&gt; Class
The Tuple&lt;T1, T2, T3, T4, T5, T6, T7, TRest&gt; class is used to create an n-tuple where n = 8 or greater than 8. It represents a tuple that contains the eight or more than eight elements in it. You can instantiate a Tuple&lt;T1, T2, T3, T4, T5, T6, T7, TRest&gt; object by calling either the Tuple&lt;T1, T2, T3, T4, T5, T6, T7, TRest&gt;(T1, T2,
4 min read
C# | Tuple Class
In C#, Tuple class is used to provide static methods for creating tuples and this class defined under System namespace. This class itself does not represent a tuple, but it provides static methods that are used to create an instance of the tuple type. Or in other words, the Tuple class provides helper methods that are used to instantiate tuple obje
3 min read
Article Tags :