Skip to content
Related Articles

Related Articles

C# | Tuple<T1,T2> Class

Improve Article
Save Article
  • Last Updated : 23 Nov, 2021
Improve Article
Save Article

The Tuple<T1, T2> 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<T1, T2> object by calling either the Tuple<T1, T2>(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 Item1 and Item2 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; }  

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

Property

PropertyDescription
Item1Gets the value of the Tuple<T1, T2> object’s first component.
Item2Gets the value of the current Tuple<T1, T2> object’s second component.

Example:

CSharp




// C# program to illustrate the constructor
// and property of Tuple<T1,T2> Class
using System;
 
class GFG {
 
    // Main Method
    static public void Main()
    {
        // Creating 2-Tuple
        // Using Tuple<T1, T2>(T1, T2) constructor
        Tuple<int, int> mytuple = new Tuple<int, int>(79, 80);
 
        // Accessing the values
        Console.WriteLine("Value of the First Component: " + mytuple.Item1);
        Console.WriteLine("Value of the Second Component: " + mytuple.Item2);
    }
}

Output: 

Value of the First Component: 79
Value of the Second Component: 80

 

Methods

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

Example:

CSharp




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

Output: 

Tuple not matched..

 

Reference: 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!