Open In App
Related Articles

C# | ValueTuple <T1,T2,T3,T4,T5,T6,T7,TRest> Struct

Improve Article
Improve
Save Article
Save
Like Article
Like

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

ConstructorDescription
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

FieldDescription
Item1Gets the value of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance’s first element.
Item2Gets the value of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance’s second element.
Item3Gets the value of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance’s third element.
Item4Gets the value of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance’s fourth element.
Item5Gets the value of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance’s fifth element.
Item6Gets the value of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance’s sixth element.
Item7Gets the value of the current ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> instance’s seventh element.
TRestAny generic value tuple instance that defines the types of the tuple’s remaining elements.

Example:




// C# program to illustrate how to
// access the element of ValueTuple<T1,
// T2, T3, T4, T5, T6, T7, TRest>
using System;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Creating a value tuple
        // Using Create method
        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"));
  
        // Display the element of the given value tuple
        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

MethodDescription
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:




// C# program to check the given 
// value tuples are equal or not
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating 8-ValueTuple
        // Using Create method
        var T1 = ValueTuple.Create(3, 2, 4, 5, 6, 7, 6, 10);
        var T2 = ValueTuple.Create(3, 2, 4, 5, 6, 7, 6, 1);
  
        // Check if both the value
        // tuples are equal or not
        if (T1.Equals(T2))
        {
            Console.WriteLine("Code is correct...!!");
        }
        else
        {
            Console.WriteLine("Incorrect Code...!!");
        }
    }
}

Output:

Incorrect Code...!!

Reference:


Last Updated : 23 Jul, 2019
Like Article
Save Article
Similar Reads