Computer >> Computer tutorials >  >> Programming >> C#

C# Program to access tuple elements


Create a tuple.

var myTuple = Tuple.Create(1, 2.5M, "Amit", "100");

Now to access tuple elements, use the properties.

To access first element.

myTuple.Item1

To access second element.

myTuple.Item2

In the same way, for other elements, use the properties as shown below −

Example

using System;
public class Program {
   public static void Main() {
      var myTuple = Tuple.Create(1, 2.5M, "Amit", "100");
      Console.WriteLine("Item1 : "+ myTuple.Item1);
      Console.WriteLine("Item2 : "+ myTuple.Item2);
      Console.WriteLine("Item3 : "+ myTuple.Item3);
      Console.WriteLine("Item4 : "+ myTuple.Item4);
   }
}

Output

Item1 : 1
Item2 : 2.5
Item3 : Amit
Item4 : 100