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, and so on.

Syntax:

public T6 Item6;

Here, T6 is the field value of a ValueTuple<> structure. This ValueTuple<> can be 6-ValueTuple, or 7-ValueTuple, or 8-ValueTuple.



Example 1: In the below code, you can see that we are accessing the sixth element of each value tuple.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate how to get
// the sixth element of value tuple
using System;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        Console.WriteLine("C# Topics:");
  
        // Creating a value tuple with six elements
        var ValTpl6 = ValueTuple.Create("Nullable Types", "Class",
            "Structure", "Indexers", "Switch Statement", "Loops");
  
        // Accessing the sixth element of 
        // 6-ValueTuple using Item property
        Console.WriteLine(ValTpl6.Item6);
  
        // Creating a value tuple with seven elements
        var ValTpl7 = ValueTuple.Create("Inheritance ", "Constructors"
                        "Encapsulation", "Abstraction", "Static Class",
                                    "Partial Classes", "this keyword");
  
        // Accessing the sixth element of 
        // 7-ValueTuple using Item property
        Console.WriteLine(ValTpl7.Item6);
  
        // Creating a value tuple with eight elements
        var ValTpl8 = ValueTuple.Create("Methods", "Method Hiding",
                        "Optional Parameters", "Anonymous Method",
                "Partial Methods", "Local Function", "Delegates",
                                                    "Destructors");
  
        // Accessing the sixth element of 
        // 8-ValueTuple using Item property
        Console.WriteLine(ValTpl8.Item6);
    }
}

chevron_right


Output:

C# Topics:
Loops
Partial Classes
Local Function

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to get the hash code of
// sixth element in a value tuple
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating 6-ValueTuple
        var My_Value_Tuple = (1004, "Rohit", "Computer Science"
                                                24, "C#", 2017);
  
        // Accessing sixth element of the value tuple
        Console.WriteLine("Passing Year: {0}"
                        My_Value_Tuple.Item6);
  
        // Getting the hashcode of the sixth element
        Console.WriteLine("Hash Code: {0}",
           My_Value_Tuple.Item6.GetHashCode());
    }
}

chevron_right


Output:

Passing Year: 2017
Hash Code: 2017


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.