Skip to content
Related Articles

Related Articles

Improve Article

How to get fifth Element of the ValueTuple in C#?

  • Last Updated : 23 Jul, 2019

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

Syntax:

public T5 Item5;

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

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




// C# program to illustrate how to get
// the fifth element of value tuple
using System;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        Console.WriteLine("C# Topics:");
  
        // Creating a value tuple with five elements
        var ValTpl5 = ValueTuple.Create("Identifiers", "Data Types",
                    "Keywords", "Access Modifiers", "Operators");
  
        // Accessing the fifth element of 
        // 5-ValueTuple using Item property
        Console.WriteLine(ValTpl5.Item5);
  
        // Creating a value tuple with six elements
        var ValTpl6 = ValueTuple.Create("Nullable Types", "Class",
            "Structure", "Indexers", "Switch Statement", "Loops");
  
        // Accessing the fifth element of 
        // 6-ValueTuple using Item property
        Console.WriteLine(ValTpl6.Item5);
  
        // Creating a value tuple with seven elements
        var ValTpl7 = ValueTuple.Create("Inheritance ", "Constructors"
                        "Encapsulation", "Abstraction", "Static Class",
                                    "Partial Classes", "this keyword");
  
        // Accessing the fifth element of 
        // 7-ValueTuple using Item property
        Console.WriteLine(ValTpl7.Item5);
  
        // 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 fifth element of 
        // 8-ValueTuple using Item property
        Console.WriteLine(ValTpl8.Item5);
    }
}
Output:
C# Topics:
Operators
Switch Statement
Static Class
Partial Methods

Example 2:




// C# program to get the hash code of
// fifth element in a value tuple
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating 5-ValueTuple
        var My_Value_Tuple = (1004, "Rohit", "Computer Science",
                                                     24, "C#");
  
        // Accessing fifth element of the value tuple
        Console.WriteLine("Favourite programming Language: {0}",
                                          My_Value_Tuple.Item5);
  
        // Getting the hashcode of the fifth element
        Console.WriteLine("Hash Code: {0}",
           My_Value_Tuple.Item5.GetHashCode());
    }
}
Output:
Favourite programming Language: C#
Hash Code: -849950870



My Personal Notes arrow_drop_up
Recommended Articles
Page :