Articles → CSHARP → Tuples In C#

Tuples In C#






Purpose





How To Declare A Tuple?




  1. Passing the value in the constructor of the tuple class
  2. Using the Create method
Tuple<string, string, int> person = new Tuple< string, string, int>("sumit", "sharma", 45);
Tuple<string, string, int> anotherPerson = Tuple.Create("sumit", "sharma", 45);



How Many Parameters Can Be Passed Inside The Create Method





Accessing The Tuple Element




var tuple = Tuple.Create(1,2,3,4,5,6,7,8);


// Accessing the elements
Console.WriteLine(tuple.Item1);
Console.WriteLine(tuple.Item2);
Console.WriteLine(tuple.Item3); 
Console.WriteLine(tuple.Item4);
Console.WriteLine(tuple.Item5);
Console.WriteLine(tuple.Item6);
Console.WriteLine(tuple.Item7);

Console.ReadLine();


Picture showing the output of tuples in C#
Click to Enlarge




Posted By  -  Karan Gupta
 
Posted On  -  Thursday, February 2, 2023

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250