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:
using System;
class GFG {
static public void Main()
{
var MyTple1 = ValueTuple.Create();
Console.WriteLine( "HashCode of a value tuple with " +
"zero elements: " + MyTple1.GetHashCode());
var MyTple2 = ValueTuple.Create(56, 3);
var MyTple3 = ValueTuple.Create(56, 45);
int res1 = MyTple2.CompareTo(MyTple3);
Console.WriteLine( "CompareTo Method Result: " + res1);
}
}
|
Output:
HashCode of a value tuple with zero elements: 0
CompareTo Method Result: -1
Reference:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
23 Jul, 2019
Like Article
Save Article