C# | How to get First 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. Item1 Property is used to get the first element of the given tuple. It is applicable on every tuple like 1-Tuple, 2-Tuple, and so on.

Syntax:

public T1 Item1 { get; }

Here, T1 is the value of the current Tuple<> object’s first component. This Tuple<> can be 1-tuple, or 2-tuple, or 3-tuple, or 4-tuple, or 5-tuple, or 6-tuple, or 7-tuple, or 8-tuple.



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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate how to get 
// the first element of the tuple
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Taking 1-tuple
        var st1 = Tuple.Create("Mohan");
        Console.WriteLine("Student-1 Name: " + st1.Item1);
  
        // Taking 2-tuple
        var st2 = Tuple.Create("Sohan", 20);
        Console.WriteLine("Student-2 Name: " + st2.Item1);
  
        // Taking 3-tuple
        var st3 = Tuple.Create("Soniya", 30, "CSE");
        Console.WriteLine("Student-3 Name: " + st3.Item1);
  
        // Taking 4-tuple
        var st4 = Tuple.Create("Rohan", 29, "CSE", 2015);
        Console.WriteLine("Student-4 Name: " + st4.Item1);
  
        // Taking 5-tuple
        var st5 = Tuple.Create("Siya", 22, "CSE", 2017,
                                           "20-Mar-1993");
  
        Console.WriteLine("Student-5 Name: " + st5.Item1);
  
        // Taking 6-tuple
        var st6 = Tuple.Create("Riya", 24, "CSE", 2015,
                               "30-May-2015", 230134832);
  
        Console.WriteLine("Student-6 Name: " + st6.Item1);
  
        // Taking 7-tuple
        var st7 = Tuple.Create("Rohit", 21, "CSE", 2017, 
                        "21-Apr-1998", 384749829, 20000);
  
        Console.WriteLine("Student-7 Name: " + st7.Item1);
  
        // Taking 8-tuple
        var st8 = Tuple.Create("Manita", 24, "CSE", 2016, 
                   "03-Aug-1991", 235678909, 34000, "C#");
  
        Console.WriteLine("Student-8 Name: " + st8.Item1);
    }
}

chevron_right


Output:

Student-1 Name: Mohan
Student-2 Name: Sohan
Student-3 Name: Soniya
Student-4 Name: Rohan
Student-5 Name: Siya
Student-6 Name: Riya
Student-7 Name: Rohit
Student-8 Name: Manita


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.