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-ValueTuple.
Syntax:
public T7 Item7;
Here, T7 is the field value of a ValueTuple<> structure. This ValueTuple<> can be 7-ValueTuple, or 8-ValueTuple.
Example 1: In the below code, you can see that we are accessing the seventh element of each value tuple.
// C# program to illustrate how to get // the seventh element of value tuple using System; class GFG { // Main Method static public void Main() { Console.WriteLine( "C# Topics:" ); // Creating a value tuple with seven elements var ValTpl7 = ValueTuple.Create( "Inheritance " , "Constructors" , "Encapsulation" , "Abstraction" , "Static Class" , "Partial Classes" , "this keyword" ); // Accessing the seventh element of // 7-ValueTuple using Item property Console.WriteLine(ValTpl7.Item7); // 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 seventh element of // 8-ValueTuple using Item property Console.WriteLine(ValTpl8.Item7); } } |
Output:
C# Topics: this keyword Delegates
Example 2:
// C# program to get the hash code of // seventh element in a value tuple using System; class GFG { // Main method static public void Main() { // Creating 7-ValueTuple var My_Value_Tuple = (1004, "Rohit" , "Computer Science" , 24, "C#" , 2017, "3-7-1993" ); // Accessing seventh element // of the value tuple Console.WriteLine( "Birth Date: {0}" , My_Value_Tuple.Item7); // Getting the hashcode of // the seventh element Console.WriteLine( "Hash Code: {0}" , My_Value_Tuple.Item7.GetHashCode()); } } |
Output:
Birth Date: 3-7-1993 Hash Code: -1761317527
Contact Us