ValueTuple Struct in C#

In C#, ValueTuple structure provides static methods that are used in creating value tuples. It is defined under System namespace. It is introduced in .NET Framework 4.7 which provides runtime implementation tuples in C#. The ValueTuple structure is used to represent a tuple which does not contain any element. Generally, it provides static methods that are used for creating or comparing value tuples. With the help of ValueTuple structure’s Create method, you can create a value tuple which holds elements starting from 0 to 8. It is different from the Tuple class in the following terms:

  • It is of value type not reference type.
  • It is mutable rather than read-only.
  • In ValueTuple, item1, item2, item3, etc. data members are fields rather than properties.

This structure implements IStructuralComparable, IStructuralEquatable, IComparable, IComparable<ValueTuple>, IEquatable<ValueTuple>, and ITuple interfaces.

Methods

Method Description
CompareTo(ValueTuple) Compares the current ValueTuple instance to a specified ValueTuple instance.
Create() Creates a new value tuple with zero components.
Create<T1, T2, T3, T4, T5, T6, T7, T8>(T1, T2, T3, T4, T5, T6, T7, T8) Creates a new value tuple with 8 components (an octuple).
Create<T1, T2, T3, T4, T5, T6, T7>(T1, T2, T3, T4, T5, T6, T7) Creates a new value tuple with 7 components (a septuple).
Create<T1, T2, T3, T4, T5, T6>(T1, T2, T3, T4, T5, T6) Creates a new value tuple with 6 components (a sexuple).
Create<T1, T2, T3, T4, T5>(T1, T2, T3, T4, T5) Creates a new value tuple with 5 components (a quintuple).
Create<T1, T2, T3, T4>(T1, T2, T3, T4) Creates a new value tuple with 4 components (a quadruple).
Create<T1, T2, T3>(T1, T2, T3) Creates a new value tuple with 3 components (a triple).
Create<T1, T2>(T1, T2) Creates a new value tuple with 2 components (a pair).
Create<T1>(T1) Creates a new value tuple with 1 component (a singleton).
Equals(ValueTuple) Determines whether two ValueTuple instances are equal. This method always returns true.
Equals(Object) Returns a value that indicates whether the current ValueTuple instance is equal to a specified object.
GetHashCode() Returns the hash code for the current ValueTuple instance.
ToString() Returns the string representation of this ValueTuple instance.

Example:



filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the 
// methods of ValueTuple struct
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating a value tuple with 
        // zero element using Create method
        var MyTple1 = ValueTuple.Create();
  
        // Using GetHashCode method
        Console.WriteLine("HashCode of a value tuple with "+
                 "zero elements: " + MyTple1.GetHashCode());
  
        // Creating a value tuple 
        var MyTple2 = ValueTuple.Create(56, 3);
        var MyTple3 = ValueTuple.Create(56, 45);
  
        // Using CompareTo method
        int res1 = MyTple2.CompareTo(MyTple3);
          
        // Display result
        Console.WriteLine("CompareTo Method Result: " + res1);
    }
}

chevron_right


Output:

HashCode of a value tuple with zero elements: 0
CompareTo Method Result: -1

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.