How to work with a Tuple in C#

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

Tuple

Tuple

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"));

This article is published as part of the IDG Contributor Network. Want to Join?

To comment on this article and other InfoWorld content, visit InfoWorld's LinkedIn page, Facebook page and Twitter stream.
From CIO: 8 Free Online Courses to Grow Your Tech Skills
Notice to our Readers
We're now using social media to take your comments and feedback. Learn more about this here.