The Wayback Machine - https://web.archive.org/web/20240907063827/https://www.geeksforgeeks.org/valuetuple-struct-in-c-sharp/
Open In App

ValueTuple Struct in C#

Last Updated : 23 Jul, 2019
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:





Output:

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

Reference:


Previous Article
Next Article

Similar Reads

C# | ValueTuple &lt;T1,T2,T3,T4,T5,T6,T7&gt; Struct
The ValueTuple &lt;T1,T2,T3,T4,T5,T6,T7&gt; Struct is used to create a septuple or 7-ValueTuple. It represents a value tuple which stores, seven elements. It provides runtime implementation of value tuples. You can create the instance of ValueTuple &lt;T1,T2,T3,T4,T5,T6,T7&gt; Struct by using ValueTuple&lt;T1, T2, T3, T4, T5, T6, T7&gt;(T1, T2, T3,
4 min read
C# | ValueTuple &lt;T1&gt; Struct
The ValueTuple &lt;T1&gt; Struct is used to create a singleton value tuple or 1-ValueTuple which stores only one component. It provides runtime implementation of value tuples. You can create the instance of ValueTuple &lt;T1&gt; Struct by using ValueTuple&lt;T1&gt;(T1) constructor, or by using ValueTuple.Create method or simply by using parenthesis
2 min read
C# | ValueTuple &lt;T1,T2&gt; Struct
The ValueTuple&lt;T1,T2&gt; Struct is used to create a pair value tuple or 2-ValueTuple. It represents a value tuple which stores, two elements. It provides runtime implementation of value tuples. You can create the instance of ValueTuple &lt;T1, T2&gt; Struct by using ValueTuple&lt;T1,T2&gt;(T1, T2) constructor, or by using ValueTuple.Create metho
3 min read
C# | ValueTuple &lt;T1,T2,T3&gt; Struct
The ValueTuple &lt;T1,T2,T3&gt; Struct is used to create a triple value tuple or 3-ValueTuple. It represents a value tuple which stores, three elements. It provides runtime implementation of value tuples. You can create the instance of ValueTuple &lt;T1,T2,T3&gt; Struct by using ValueTuple&lt;T1, T2, T3&gt;(T1, T2, T3) constructor, or by using Valu
3 min read
C# | ValueTuple &lt;T1,T2,T3,T4&gt; Struct
The ValueTuple &lt;T1,T2,T3,T4&gt; Struct is used to create a quadruple or 4-ValueTuple. It represents a value tuple which stores, four elements. It provides runtime implementation of value tuples. You can create the instance of ValueTuple &lt;T1,T2,T3,T4&gt; Struct by using ValueTuple&lt;T1, T2, T3, T4&gt;(T1, T2, T3, T4) constructor, or by using
3 min read
C# | ValueTuple &lt;T1,T2,T3,T4,T5&gt; Struct
The ValueTuple &lt;T1,T2,T3,T4,T5&gt; Struct is used to create a quintuple or 5-ValueTuple. It represents a value tuple which stores, five elements. It provides runtime implementation of value tuples. You can create the instance of ValueTuple &lt;T1,T2,T3,T4,T5&gt; by using ValueTuple&lt;T1, T2, T3, T4, T5&gt;(T1, T2, T3, T4, T5) constructor, or by
4 min read
C# | ValueTuple &lt;T1,T2,T3,T4,T5,T6&gt; Struct
The ValueTuple &lt;T1,T2,T3,T4,T5,T6&gt; Struct is used to create a sextuple or 6-ValueTuple. It represents a value tuple which stores, six elements. It provides runtime implementation of value tuples. You can create the instance of ValueTuple &lt;T1,T2,T3,T4,T5,T6&gt; Struct Struct by using ValueTuple&lt;T1, T2, T3, T4, T5, T6&gt;(T1, T2, T3, T4,
4 min read
C# | ValueTuple &lt;T1,T2,T3,T4,T5,T6,T7,TRest&gt; Struct
The ValueTuple &lt;T1,T2,T3,T4,T5,T6,T7,TRest&gt; Struct is used to create an n-ValueTuple where n = 8 or greater than 8. It represents a value tuple which contains the eight or more than eight elements in it. It provides runtime implementation of value tuples. You can create the instance of ValueTuple &lt;T1,T2,T3,T4,T5,T6,T7,TRest&gt; Struct by u
4 min read
Checking if two ValueTuple&lt;T1,T2,T3,T4&gt; are equal or not in C#
ValueTuple is a structure introduced in C# 7.0 which represents the value type Tuple. It is already included in .NET Framework 4.7 or higher version. It allows you to store a data set that contains multiple values that may or may not be related to each other. In ValueTuple&lt;T1, T2, T3, T4&gt;, you can check if two value tuples are identical or no
3 min read
Check if ValueTuple instances are equal in C#
ValueTuple is a structure introduced in C# 7.0 which represents the value type Tuple. It allows you to store a data set which contains multiple values that may or may not be related to each other. In value tuples, you can check if two value tuples are identical or not by using the ValueTuple.Equals Method. This method will return value which indica
2 min read
ValueTuple in C#
ValueTuple is a structure introduced in C# 7.0 which represents the value type Tuple. It is already included in .NET Framework 4.7 or higher version. It allows you to store a data set which contains multiple values that may or may not be related to each other. It can store elements starting from 0 to 8 and can store elements of different types. You
7 min read
How to create 1-ValueTuple in C#?
In C#, a Singleton or 1-ValueTuple is a value type tuple which holds only one Component. You can create a singleton value tuple using two different ways: Using ValueTuple &lt;T1&gt;(T1) Constructor Using Create &lt;T1&gt;(T1) Method Using ValueTuple &lt;T1&gt;(T1) Constructor You can create a singleton value tuple using ValueTuple &lt;T1&gt;(T1) co
2 min read
How to create 2-ValueTuple in C#?
In C#, a pair or 2 value tuple is a value type tuple which holds two elements in it. You can create a pair of value tuple using two different ways: Using ValueTuple &lt;T1, T2&gt;(T1, T2) Constructor Using Create &lt;T1, T2&gt;(T1, T2) Method Using ValueTuple &lt;T1, T2&gt;(T1, T2) Constructor You can create a pair value tuple by using ValueTuple
2 min read
How to create 4-ValueTuple in C#?
In C#, a quadruple or 4 value tuple is a value type tuple which holds four elements in it. You can create a quadruple value tuple using two different ways: Using ValueTuple &lt;T1, T2, T3, T4&gt;(T1, T2, T3, T4) Constructor Using Create &lt;T1, T2, T3, T4&gt;(T1, T2, T3, T4) method Using ValueTuple &lt;T1, T2, T3, T4&gt;(T1, T2, T3, T4) Constructor
3 min read
How to create 3-ValueTuple in C#?
In C#, a triple or 3 value tuple is a value type tuple which holds three elements in it. You can create a triple value tuple using two different ways: Using ValueTuple &lt;T1, T2, T3&gt;(T1, T2, T3) Constructor Using Create &lt;T1, T2, T3&gt;(T1, T2, T3) Method Using ValueTuple &lt;T1, T2, T3&gt;(T1, T2, T3) Constructor You can create a triple valu
3 min read
How to create 5-ValueTuple in C#?
In C#, a 5-ValueTuple or quintuple is a value type tuple which contains five elements. You can create a 5-ValueTuple using two different ways: Using ValueTuple &lt;T1, T2, T3, T4, T5&gt;(T1, T2, T3, T4, T5) Constructor Using Create &lt;T1, T2, T3, T4, T5&gt;(T1, T2, T3, T4, T5) Method Using ValueTuple &lt;T1, T2, T3, T4, T5&gt;(T1, T2, T3, T4, T5)
4 min read
How to create 6-ValueTuple in C#?
In C#, a 6-ValueTuple or sextuple is a value type tuple which contains six elements. You can create a 6-ValueTuple using two different ways: Using ValueTuple &lt;T1, T2, T3, T4, T5, T6&gt;(T1, T2, T3, T4, T5, T6) Constructor Using Create &lt;T1, T2, T3, T4, T5, T6&gt;(T1, T2, T3, T4, T5, T6) Method Using ValueTuple &lt;T1, T2, T3, T4, T5, T6&gt;(T1
4 min read
How to create 7-ValueTuple in C#?
In C#, a 7-ValueTuple is a value type tuple which contains seven elements and it is also known as septuple. You can create a 7-ValueTuple using two different ways: Using ValueTuple &lt;T1, T2, T3, T4, T5, T6, T7&gt;(T1, T2, T3, T4, T5, T6, T7) Constructor Using Create &lt;T1, T2, T3, T4, T5, T6, T7&gt;(T1, T2, T3, T4, T5, T6, T7) Method Using Value
4 min read
How to create 8-ValueTuple in C#?
In C#, an 8-ValueTuple is a value type tuple which contains eight elements and it is also known as octuple. You can create an 8-ValueTuple using two different ways: Using ValueTuple&lt;T1, T2, T3, T4, T5, T6, T7, TRest&lt;(T1, T2, T3, T4, T5, T6, T7, TRest) Constructor Using Create&lt;T1, T2, T3, T4, T5, T6, T7, T8&lt;(T1, T2, T3, T4, T5, T6, T7, T
5 min read
How to compare two ValueTuple in C#?
To compare two instances of ValueTuple you can use CompareTo method which is provided by ValueTuple structure. ValueTuple.CompareTo(ValueTuple) Method is used to compare the current instance of ValueTuple with another ValueTuple instance. It always returns zero if they are equal to each other. Syntax: public int CompareTo (ValueTuple other); Here,
2 min read
Getting the hash code of the ValueTuple in C#
ValueTuple.GetHashCode Method is used to get the HashCode of the current ValueTuple instance. It is provided by the ValueTuple struct. Syntax: public override int GetHashCode (); Returns: The return type of this method is System.Int32 and it always returns zero. Example: // C# program to illustrate how to // find the hash code of the given // value
2 min read
How to get Second Element of the ValueTuple in C#?
ValueTuple is a structure introduced in C# 7.0 which represents the value type Tuple. It allows you to store a data set which contains multiple values that may or may not be related to each other. Item2 Property is used to get the second unnamed element of the given value tuple. It is applicable on every value tuple like 2-ValueTuple, 3-ValueTuple,
2 min read
How to get fifth Element of the ValueTuple in C#?
ValueTuple is a structure introduced in C# 7.0 which represents the value type Tuple. It allows you to store a data set which contains multiple values that may or may not be related to each other. Item5 Property is used to get the fifth unnamed element of the given value tuple. It is applicable on every value tuple like 5-ValueTuple, 6-ValueTuple,
2 min read
How to get First Element of the ValueTuple in C#?
ValueTuple is a structure introduced in C# 7.0 which represents the value type Tuple. It allows you to store a data set which contains multiple values that may or may not be related to each other. Item1 Property is used to get the first unnamed element of the given value tuple. It is applicable on every value tuple like 1-ValueTuple, 2-ValueTuple,
2 min read
How to get third Element of the ValueTuple in C#?
ValueTuple is a structure introduced in C# 7.0 which represents the value type Tuple. It allows you to store a data set which contains multiple values that may or may not be related to each other. Item3 Property is used to get the third unnamed element of the given value tuple. It is applicable on every value tuple like 3-ValueTuple, 4-ValueTuple,
2 min read
How to get fourth Element of the ValueTuple in C#?
ValueTuple is a structure introduced in C# 7.0 which represents the value type Tuple. It allows you to store a data set which contains multiple values that may or may not be related to each other. Item4 Property is used to get the fourth unnamed element of the given value tuple. It is applicable on every value tuple like 4-ValueTuple, 5-ValueTuple,
2 min read
How to get sixth Element of the ValueTuple in C#?
ValueTuple is a structure introduced in C# 7.0 which represents the value type Tuple. It allows you to store a data set which contains multiple values that may or may not be related to each other. Item6 Property is used to get the sixth unnamed element of the given value tuple. It is applicable on every value tuple like 6-ValueTuple, 7-ValueTuple,
2 min read
How to get seventh Element of the ValueTuple in C#?
ValueTuple is a structure introduced in C# 7.0 which represents the value type Tuple. It allows you to store a data set which contains multiple values that may or may not be related to each other. Item7 Property is used to get the seventh unnamed element of the given value tuple. It is applicable on every value tuple like 7-ValueTuple and 8-ValueTu
2 min read
How to get the Elements of the Nested ValueTuple in C#?
ValueTuple is a structure introduced in C# 7.0 which represents the value type Tuple. It allows you to store a data set which contains multiple values that may or may not be related to each other. As we know that Item&lt;ElementNumber&gt; property is used to get the elements present in the value tuples, but this property works only for seven elemen
3 min read
How to Get the String that Represents ValueTuple Instance in C#?
ValueTuple is a structure introduced in C# 7.0 which represents the value type Tuple. It allows you to store a data set that contains multiple values that may or may not be related to each other. You can also get a string that represents the object of ValueTuple, with the help of the ToString Method. This method returns a string that will represent
4 min read
Article Tags :