Tuple types in C# is a data structure that contains a sequence of elements of similar or different data types, it was introduced in C#.Net 4.0
C# Tuple is also known as immutable data type similar to anonymous types , that means once a Tuple type is created then all the properties of the Tuple types becomes read-only.
How to declare a C# Tuple type :
We can create a Tuple type by using Generic syntax as Tuple<T>, where T is the data type . Lets have a look at below example of a simple Tuple type.
1 |
Tuple<string> t = new Tuple<string>("Demo Tuple"); |
Here in above, we have declared a Tuple type t that has a single string item as “Demo Tuple”. Lets look at below example Tuple that has 3 elements in it.
1 |
Tuple<string, string, decimal> student = new Tuple<string, string, decimal>("Amit Kumar", "Physics", 87.5M); |
We can also use the implicitly typed variable (Var) to create above Tuple type as follows.
1 |
var student = new Tuple<string, string, decimal>("Amit Kumar", "Physics", 87.5M); |
Accessing Tuple items in C# :
Once you declare a Tuple type , then the elements can be accessed with Item<ElementNumber>, e.g., Item1, Item2, and so on up to Item7 property. The Item1 property returns the first element, Item2 returns the second element, and so on.
For example, we can access above declared tuple object Tuple<string, string, decimal> student properties as follows.
1 2 3 |
Console.WriteLine("Name : {0}", student.Item1); //Prints "Amit Kumar" Console.WriteLine("Subject : {0}", student.Item2); //Prints "Physics" Console.WriteLine("Mark : {0}", student.Item3); //Prints "87.5" |
Tuple declaration with 3 elements example :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using System; namespace TupleTypeDemo { class Program { static void Main(string[] args) { //Tuple object of 3 items Tuple<string, string, double> student = new Tuple<string, string, double>("Amit Kumar", "Physics", 87.5); //Access the student tuple object as follows Console.WriteLine("Print Student Tuple object"); Console.WriteLine("***************************"); Console.WriteLine("Name : {0}", student.Item1); Console.WriteLine("Subject : {0}", student.Item2); Console.WriteLine("Mark : {0}", student.Item3); } } } |
Output to above sample code.
Tuple declaration with 4 elements example :
Lets look at below example of a tuple declaration having 4 elements of different type.
- We have declared a tuple object as var employee = new Tuple<int, string, string[], int[]> having items as int, string , string array and an integer array.
- Again we are accessing the tuple items by looping thorough the array items and displaying it in the console window.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
using System; namespace TupleTypeDemo { class Program { static void Main(string[] args) { //Employee tuple object of 4 items var employee = new Tuple<int, string, string[], int[]>(1001, "Amit Kumar", new string[] { "VB.Net", "C#.Net" }, new int[] { 4, 5 }); //Access the Employee tuple object as follows Console.WriteLine("Print Employee Tuple object"); Console.WriteLine("***************************"); Console.WriteLine("ID : {0}", employee.Item1); Console.WriteLine("Name : {0}", employee.Item2); //Print array items present in employee.Item3 Console.WriteLine("Programing Language : "); foreach (string value in employee.Item3) { Console.WriteLine(value); } //Print array items present in employee.Item4 Console.WriteLine("Years Of Exp : "); foreach (int value in employee.Item4) { Console.WriteLine(value); } } } } |
Output to above example :
Create C# tuple using Tuple.Create() method :
The System namespace has a non generic static Tuple class, which has number of static methods as Create , which acts as a factory method for creating new Tuple objects.
1 2 3 |
//Tuple object creation using Create() method var student = Tuple.Create("Amit Kumar", "Physics", 87.5M); var employee = Tuple.Create(1001, "Amit Kumar", new string[] { "VB.Net", "C#.Net" }, new int[] { 4, 5 }); |
Nested Tuple types in C#:
Tuple<T> generic type supports item1, item2, item3 upto item7 elements, that can hold 7 element of similar or different data types.
If you want to create a new C# Tuple type to hold 8 or more elements, then you can do that by nesting another Tuple type in the last argument TRest property of Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> object.
In below example, we are nesting a new Tuple object new Tuple(8,9,10) as TRest parameter to the parent Tuple type.
1 |
var tuple = Tuple.Create(1,2,3,4,5,6,7, new Tuple(8,9,10)); |
Above declaration creates a nested Tuple type of 10 elements , You can access above Tuple object as follows.
1 2 3 4 5 6 7 8 9 10 |
Console.WriteLine("tuple.Item1 : {0}", tuple.Item1); //prints 1 Console.WriteLine("tuple.Item2 : {0}", tuple.Item2); //prints 2 Console.WriteLine("tuple.Item3 : {0}", tuple.Item3); //prints 3 Console.WriteLine("tuple.Item4 : {0}", tuple.Item4); //prints 4 Console.WriteLine("tuple.Item5 : {0}", tuple.Item5); //prints 5 Console.WriteLine("tuple.Item6 : {0}", tuple.Item6); //prints 6 Console.WriteLine("tuple.Item7 : {0}", tuple.Item7); //prints 7 Console.WriteLine("tuple.Rest.Item1.Item1 : {0}", tuple.Rest.Item1.Item1); //prints 8 Console.WriteLine("tuple.Rest.Item1.Item2 : {0}", tuple.Rest.Item1.Item2); //prints 9 Console.WriteLine("tuple.Rest.Item1.Item3 : {0}", tuple.Rest.Item1.Item3); //prints 10 |
Key Points :
Following are some of the key points about Tuple Types.
- Tuple type as function parameter : Tuple types are useful in situations, where instead of sending multiple method arguments, you can send a single Tuple object having all that elements in it. This prevents creating a new class or structure and gives a shortcut option.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using System; namespace TupleTypeDemo { class Program { static void Main(string[] args) { var student = new Tuple<string, string, decimal>("Amit", "Physics", 87.5M); //Method calling with tuple type as a parameter DisplayStudentInfo(student); } /// <summary> /// Receives a tuple type as parameter and access its properties /// </summary> /// <param name="student"></param> public static void DisplayStudentInfo(Tuple<string,string,decimal> student) { Console.WriteLine("Name : {0}", student.Item1); //Prints "Amit" Console.WriteLine("Subject : {0}", student.Item2); //Prints "Physics" Console.WriteLine("Mark : {0}", student.Item3); //Prints "87.5" } } } |
- Tuple type as function Return type : In situations, where you want multiple values as return type from a function. We can use a single tuple object instead of having multiple out or ref as parameter type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
using System; namespace TupleTypeDemo { class Program { static void Main(string[] args) { //Method return a tuple type var student = FetchStudentInfo(); Console.WriteLine("Name : {0}", student.Item1); //Prints "Amit" Console.WriteLine("Subject : {0}", student.Item2); //Prints "Physics" Console.WriteLine("Mark : {0}", student.Item3); //Prints "87.5" } /// <summary> /// Returns a tuple type as return type /// </summary> /// <param name="student"></param> public static Tuple<string, string, decimal> FetchStudentInfo() { var student = new Tuple<string, string, decimal>("Amit", "Physics", 87.5M); return student; } } } |
- The Tuple object properties are accessible using the name pattern like Item1, Item2 etc , which doesn’t make any sense. This creates code readability and maintainability issue.
- The Tuple types are reference types and that might create performance issue.
Tuple types in C# can be useful in some situations, however they have some limitations and constraints. In order to overcome this limitations, C# 7 came up with a new feature as ValueTuple . We will be discussing more about it in our next article.
Thanks for your time feel free to provide your feedback and do check my other blogs on Dynamic type in C# , Nullable type in C# , Anonymous type in C# and Var Type in C#