C# | ValueTuple <T1,T2,T3,T4,T5,T6,T7,TRest> Struct
The ValueTuple <T1,T2,T3,T4,T5,T6,T7,TRest> 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 <T1,T2,T3,T4,T5,T6,T7,TRest> Struct by using ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>(T1, T2, T3, T4, T5, T6, T7, TRest) constructor, or by using ValueTuple.Create Method or simply by using parenthesis(). You can retrieve the value of the value tuple’s unnamed elements by using the default property, or named elements can directly be accessed with the help of their names.
Important Points:
- It implements IStructuralComparable, IStructuralEquatable, IComparable, IComparable<ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>>, IEquatable<ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>>, and ITuple interfaces.
- It defined under System namespace.
- It can also store duplicate elements.
- Fields are mutable. So, you can change the value of ValueTuple <T1, T2, T3, T4, T5, T6, T7, TRest>.
- Here, members like Item1, Item2, Item3, Item4, Item5, Item6, Item7, etc. are fields not property.
- It is of value type not of reference type.
- It represent multiple data into a single data set.
- It allows passing multiple values to a method with the help of single parameters.
Constructor
Constructor | Description |
---|
ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>(T1, T2, T3, T4, T5, T6, T7, TRest) | Initializes a new ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance. |
Field
Field | Description |
---|
Item1 | Gets the value of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance’s first element. |
Item2 | Gets the value of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance’s second element. | Item3 | Gets the value of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance’s third element. | Item4 | Gets the value of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance’s fourth element. | Item5 | Gets the value of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance’s fifth element. | Item6 | Gets the value of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance’s sixth element. | Item7 | Gets the value of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance’s seventh element. | TRest | Any generic value tuple instance that defines the types of the tuple’s remaining elements. |
Example:
using System;
class GFG {
static public void Main()
{
var Mylibrary = ValueTuple.Create(3456, "The Guide" , "R. K. Narayan" ,
1958, "Philosophical novel" , "English" , "India" ,
ValueTuple.Create( "Swami and Friends" , "The Dark Room" ,
"Mr. Sampath" , "Grandmother's Tale" ));
Console.WriteLine( "Book Details: " );
Console.WriteLine( "Book Id: {0}" , Mylibrary.Item1);
Console.WriteLine( "Book Name: {0}" , Mylibrary.Item2);
Console.WriteLine( "Author Name: {0}" , Mylibrary.Item3);
Console.WriteLine( "Publication date: {0}" , Mylibrary.Item4);
Console.WriteLine( "Gener: {0}" , Mylibrary.Item5);
Console.WriteLine( "Language: {0}" , Mylibrary.Item6);
Console.WriteLine( "Country: {0}" , Mylibrary.Item7);
Console.WriteLine( "Other Novels: {0}" , Mylibrary.Rest);
}
}
|
Output:
Book Details:
Book Id: 3456
Book Name: The Guide
Author Name: R. K. Narayan
Publication date: 1958
Gener: Philosophical novel
Language: English
Country: India
Other Novels: ((Swami and Friends, The Dark Room, Mr. Sampath, Grandmother's Tale))
Methods
Method | Description |
---|
CompareTo(ValueTuple) | Compares the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance to a specified ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance. |
Equals(Object) | Returns a value that indicates whether the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance is equal to a specified object. |
Equals(ValueTuple) | Returns a value that indicates whether the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance is equal to a specified ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance. |
GetHashCode() | Calculates the hash code for the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance. |
ToString() | Returns a string that represents the value of this ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance. |
Example:
using System;
class GFG {
static public void Main()
{
var T1 = ValueTuple.Create(3, 2, 4, 5, 6, 7, 6, 10);
var T2 = ValueTuple.Create(3, 2, 4, 5, 6, 7, 6, 1);
if (T1.Equals(T2))
{
Console.WriteLine( "Code is correct...!!" );
}
else
{
Console.WriteLine( "Incorrect Code...!!" );
}
}
}
|
Output:
Incorrect Code...!!
Reference: