C# | How to get Seventh Element of the Tuple?

Tuple is a data structure which gives you the easiest way to represent a data set which has multiple values that may/may not be related to each other. Item7 Property is used to get the seventh element of the given tuple. It is not applicable on 1-Tuple, 2-Tuple, 3-Tuple, 4-Tuple, 5-Tuple, and 6-tuple but applicable on all other remaining tuples.

Syntax:

public T7 Item7 { get; }

Here, T7 is the value of the current Tuple<> object’s seventh component. This Tuple<> can be 7-tuple, or 8-tuple.



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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate how to get 
// the seventh element of the tuple
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
  
        // Taking 7-tuple
        var st7 = Tuple.Create("Rohit", 21, "IT", 2017, 
                        "21-Apr-1994", 384749829, 1);
  
        Console.WriteLine("Student-7 Position.: " + st7.Item7);
  
        // Taking 8-tuple
        var st8 = Tuple.Create("Manita", 24, "CSE", 2013, 
                   "03-Aug-1991", 235678909, 2, "C#");
  
        Console.WriteLine("Student-8 Position: " + st8.Item7);
    }
}

chevron_right


Output:

Student-7 Position.: 1
Student-8 Position: 2

Note: You can also get the type of the Seventh component of the tuple by using GetType() method or by using Type.GetGenericArguments method.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate how to get the 
// type of the Seventh element of the tuple
using System;
   
class GFG{
       
    // Main method
    static public void Main (){
           
        // Taking 7-tuple
        var stu7 = Tuple.Create("Riya", 24, "CSE", 2015, 102, 230134832, "DSA");
        Console.WriteLine("Student-7 Favourite Subject: "+stu7.Item7);
           
        // Get the type of Item7
        // Using GetType() method
        Console.WriteLine("Type of Item7: "+stu7.Item7.GetType());
           
        // Get the type of Item7
        // Using Type.GetGenericArguments method
        Type stu7type = stu7.GetType();
        Console.WriteLine("Type of Item7: "+stu7type.GetGenericArguments()[6]);
  
        Console.WriteLine();
          
    }
}

chevron_right


Output:

Student-7 Favourite Subject: DSA
Type of Item7: System.String
Type of Item7: System.String


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.