C# Tuple
C# has included tuple support through the .Net Framework 4.0. Tuples enables us to store or pass two or more values without declaring a structure or class. It can be used where you want to have a data structure to hold an object with properties, but you don’t want to create a separate type for it. A tuple is useful when you need to pass a data set as a single parameter of a method without using ref and out parameter.
Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>
For Example :
1 2 | //Create a 3-tuple to store name, phone and age var employee = new Tuple<string, string, int>( "John", "+122233939", 28); |
In the following way values present in a tuple can be extracted :
1 2 | Console.WriteLine("Employee Name:{0}, Phone:{1}, Age:{2}.", employee.Item1, employee.Item2, employee.Item3); |
To return a tuple from a method:
1 2 3 4 5 6 7 8 9 | static void Main(string[] args) { var employee = GetEmployee(); } static Tuple<string, string, int> GetEmployee() { return Tuple.Create("john", "+192323234" 28); } |