C# | Tuple<T1> Class

The Tuple<T1> class is used to create 1-tuple or singleton which contains only a single element in it. You can instantiate a Tuple <T1> object by calling either the Tuple<T1> 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.

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

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

Property

Property Description
Item1 Gets the value of the Tuple<T1> object’s single element.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the constructor 
// and property of class Tuple<T1>
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating 1-Tuple
        // Using Tuple<T1>(T1)
        Tuple<int> mytuple = new Tuple<int>(22);
  
        // Accessing the values
        Console.WriteLine("Value of the Element is: " + mytuple.Item1);
    }
}

chevron_right


Output:

Value of the Element is: 22

Methods

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

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Tuple Matched..

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.