Joydip Kanjilal
Contributor

How to work with a Tuple in C#

opinion
Jul 15, 20164 mins
Software Development

Tuples can be used to store a finite sequence of homogeneous or heterogeneous data of fixed sizes and can be used to return multiple values from a method

Tuples are nothing new – they have been around for quite some time now in programming languages like F#, Python, etc. and also in databases. A Tuple maybe be defined as a data structure that comprises of an ordered, finite sequence of immutable, heterogeneous elements that are of fixed sizes. Elements in a Tuple are immutable, i.e., they pertain to a specific type. You can take advantage of tuples to return multiple values from a method and also create composite collections. You can leverage the static methods of the System.Tuple class to work with tuples in .Net.

What is a Tuple?

Tuple is a data structure that comprises of an ordered, heterogeneous collection of “n” elements — the elements in a tuple can either be of the same type or even be of dissimilar types. In Mathematics, an n-tuple maybe defined as a sequence or as an ordered list of “n” elements. It should be noted that “n” here denotes a positive integer. Further, there can be only one 0-tuple, i.e., an empty sequence.

The order of the elements in a Tuple is defined at the time when the Tuple is created. The properties in a Tuple are all read-only, i.e., they cannot be changed once they ahve been created. The size of the Tuple is fixed since it cannot be changed once it has been defined at the time when the Tuple is created.

Why should we use Tuples?

You may want to use a tuple to represent a set of heterogeneous data and provide an easy way to access that data. You can also take advantage of a tuple to return multiple values from a method or even pass multiple values to a method. I use a tuple to combine multiple values (if they are unrelated to each other) into one sans the need of having to use a custom class. Note that although anonymous types have many things in common, you cannot return an anonymous type from a method.

One major constraint of using tuples in this case is that you cannot have meaningful names of the properties in a Tuple — they would be named as Item1, Item2, Item3, and so on. However, as your code that uses Tuple grows, this would become unreadable and difficult to maintain over time. It should also be noted that Tuple is a class and not a struct. Hence, instances of Tuple are always stored in the managed heap. This might even become a performance challenge for you if instances of Tuple are large in sizes and they aren’t cleaned up judiciously. Here’s an interesting read on Tuple at MSDN.

Programming Tuples in C#

To work with tuples in C#, you need to leverage the Tuple class. The Tuple class is static in nature and consists provides the static Create method that can be used to create a tuple instance. Incidentally, the static Create method of the Tuple class contains eight overloads that accept generic arguments. Here’s the list of the overloaded Create methods of this class.

Tuple.Create<t1>

Tuple.Create<t1,t2>

Tuple.Create<t1,t2,t3>

Tuple.Create<t1,t2,t3,t4>

Tuple.Create<t1,t2,t3,t4,t5>

Tuple.Create<t1,t2,t3,t4,t5,t6>

Tuple.Create<t1,t2,t3,t4,t5,t6,t7>

Tuple.Create<t1,t2,t3,t4,t5,t6,t7,t8>

The following code snippet shows how you can create and initialize a Tuple.

var listEmployee = new List<Tuple<int, string, string>>

{

                Tuple.Create(1, "Joydip Kanjilal", "INDIA"),

                Tuple.Create(2, "Michael Stevens", "USA" ),

                Tuple.Create(3, "Steve Barnes", "USA" )

};

Once your Tuple is created and initialized, you can iterate it much the same way you would with a collection. The following code snippet shows how this can be achieved.

foreach(Tuple<int, string, string> tuple in listEmployee)

           {

               Console.WriteLine(tuple.Item2);

           }

And, here’s the complete code listing that illustrates how you can create, initialize and then iterate a Tuple in C#.

static void Main(string[] args)

       {

           var listEmployee = new List<Tuple<int, string, string>>

           {

               Tuple.Create(1, "Joydip Kanjilal", "INDIA"),

               Tuple.Create(2, "Michael Stevens", "USA" ),

               Tuple.Create(3, "Steve Barnes", "USA" )

           };

           foreach(Tuple<int, string, string> tuple in listEmployee)

           {

               Console.WriteLine(tuple.Item2);

           }

           Console.Read();

       }

You can also create a nested tuple. The following code snippet illustrates how you can do this.

var tuple = Tuple.Create(1,"Joydip Kanjilal",new Tuple<string,string>("Hyderabad","India"));

Joydip Kanjilal
Contributor

Joydip Kanjilal is a Microsoft Most Valuable Professional (MVP) in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP award for 2007, 2008, 2009, 2010, 2011, and 2012.

He has more than 20 years of experience in IT, with more than 16 years in Microsoft .Net and related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times.

He is the author of eight books and more than 500 articles. Many of his articles have been featured at Microsoft’s Official Site on ASP.Net.

He was a speaker at the Spark IT 2010 event and at the Dr. Dobb’s Conference 2014 in Bangalore. He has also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He is a regular speaker at the SSWUG Virtual Conference, which is held twice each year.

More from this author