C# | How to get Fifth 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. Item5 Property is used to get the fifth element of the given tuple. It is not applicable on 1-Tuple, 2-Tuple, 3-Tuple, and 4-Tuple but applicable on all other remaining tuples.

Syntax:

public T5 Item5 { get; }

Here, T5 is the value of the current Tuple<> object’s fifth component. This Tuple<> can be 5-tuple, or 6-tuple, or 7-tuple, or 8-tuple.



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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate how to get 
// the fifth element of the tuple
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Taking 5-tuple
        var st5 = Tuple.Create("Siya", 22, "EEE", 2017,
                                           "20-Mar-1993");
  
        Console.WriteLine("Student-5 DOB: " + st5.Item5);
  
        // Taking 6-tuple
        var st6 = Tuple.Create("Riya", 24, "ME", 2015,
                               "30-May-1991", 230134832);
  
        Console.WriteLine("Student-6 DOB: " + st6.Item5);
  
        // Taking 7-tuple
        var st7 = Tuple.Create("Rohit", 21, "IT", 2017, 
                        "21-Apr-1994", 384749829, 20000);
  
        Console.WriteLine("Student-7 DOB: " + st7.Item5);
  
        // Taking 8-tuple
        var st8 = Tuple.Create("Manita", 24, "CSE", 2013, 
                   "03-Aug-1991", 235678909, 34000, "C#");
  
        Console.WriteLine("Student-8 DOB: " + st8.Item5);
    }
}

chevron_right


Output:

Student-5 DOB: 20-Mar-1993
Student-6 DOB: 30-May-1991
Student-7 DOB: 21-Apr-1994
Student-8 DOB: 03-Aug-1991

Note: You can also get the type of the fifth 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 fifth element of the tuple
using System;
   
class GFG{
       
    // Main method
    static public void Main (){
           
           
        // Taking 5-tuple
        var stu5 = Tuple.Create("Siya", 22, "CSE",2017,101);
        Console.WriteLine("Student-5 ID: "+stu5.Item5);
           
        // Get the type of Item5
        // Using GetType() method
        Console.WriteLine("Type of Item5: "+stu5.Item5.GetType());
           
        // Get the type of Item5
        // Using Type.GetGenericArguments method
        Type stu5type = stu5.GetType();
        Console.WriteLine("Type of Item5: "+stu5type.GetGenericArguments()[4]);
         
    }
}

chevron_right


Output:

Student-5 ID: 101
Type of Item5: System.Int32
Type of Item5: System.Int32


My Personal Notes arrow_drop_up

Recommended Posts:



    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.