Click here to Skip to main content
15,891,828 members
Articles / Programming Languages / C# 4.0

C# 4 - Tuples

Rate me:
Please Sign up or sign in to vote.
4.81/5 (92 votes)
9 May 2011CPOL5 min read 280K   104   61
Tuples can be very handy for developers, allowing them to return multiple values from a function, the creation of composite keys to Dictionaries and eliminates structs or classes just to fill combobox.

Introduction

Basically, a tuple (Tuple in C#) is an ordered sequence, immutable, fixed-size and of heterogeneous objects, i.e., each object being of a specific type.

The tuples are not new in programming. They are already used in F#, Python and databases. However, they are new to C#. The tuples were introduced in C# 4.0 with dynamic programming.

To learn more, visit: http://msdn.microsoft.com/en-us/library/system.tuple.aspx.

Background

A) "Each line that consists of an ordered list of columns is a record or tuple. The records may not contain information on all columns, and may take null values when this becomes necessary."

http://pt.wikipedia.org/wiki/Banco_de_dados_relacional#Registros_.28ou_tuples.29

Example

SQL
Insert Into Tb_clients values (1,’Frederico’, ‘1975-03-24’)

In this example, (1,’Frederico’, ‘1975-03-24’) is a tuple.

B) “A tuple, in mathematics, is a fixed-size ordered sequence of objects”

http://es.wikipedia.org/wiki/Tuple

Example: In the equation, 2x2 - 4x - 3, the sequence (2, -4, -3) is a tuple.

C) "An enupla (also known as n-tuple) is an ordered sequence of n elements, which can be defined by the ordered pair of recursion.

The main properties that distinguish an enupla are:

  • An enupla can contain an object more than once.
  • Objects are necessarily represented in the given order. "

http://pt.wikipedia.org/wiki/Tuple

Tuples in .NET 4.0

While anonymous types have similar functionality in C#, they cannot be used as return of methods, as can the type Tuple.

The KeyValuePair<TKey, TValue> can be compared to a tuple<T1, T2>, a significant difference is that KeyValuePair is a struct and a Tuple is a class.

A tuple is an ordered sequence, immutable, fixed size of heterogeneous objects.

Ordered sequence:

The order of items in a tuple follows the order used at the time of its creation.

Immutable:

All properties are read-only tuple, i.e., once created, it cannot be changed.

Fixed Size:

The size is set at the time of its creation. If it was created with three items, you cannot add new items.

Of heterogeneous objects:

Each item has a specific and independent of the type of the other item.

Disadvantages

As Tuples don’t have an explicit semantic meaning, your code becomes unreadable.

Creating Tuples

In C#, Tuple is a static class that implements the "Factory" Pattern to create instances of Tuples. We can create an instance of a Tuple using the constructor or the static method "Create".

The static method "Create" that returns an instance of type Tuple has eight overloads:

Overload

Description

Create<T1>(T1)

Create 1-tuple, or singleton.

Create<T1, T2>(T1, T2)

Create 2-tuple, or pair.

Create<T1, T2, T3>(T1, T2, T3)

Create 3-tuple, or triple.

Create<T1, T2, T3, T4>(T1, T2, T3, T4)

Create 4-tuple, or quadruple.

Create<T1, T2, T3, T4, T5>(T1, T2, T3, T4, T5)

Create 5-tuple, or quintuple.

Create<T1, T2, T3, T4, T5, T6>(T1, T2, T3, T4, T5, T6)

Create 6-tuple, or sextuple.

Create<T1, T2, T3, T4, T5, T6, T7>(T1, T2, T3, T4, T5, T6, T7)

Create 7-tuple, or septuple.

Create<T1, T2, T3, T4, T5, T6, T7, T8>(T1, T2, T3, T4, T5, T6, T7, T8)

Create 8-tuple, or octuple.

Example

C#
Tuple<int, string, DateTime> _cliente = 
                Tuple.Create(1, "Frederico", new DateTime(1975, 3,24)); 

Tuples have a limit of 8 items. If you want to create a tuple with more items, we have to create nested Tuples.

The eighth item of the tuple has necessarily to be another Tuple. The example below will generate an exception.

C#
// Error: The eighth element must be a tuple.
var t8 = new Tuple<int,int,int,int,int,int,int,int>(1, 2, 3, 4, 5, 6, 7, 8);  

To create a tuple with 8 items, we must do the following:

C#
var t8 = new Tuple<int,int,int,int,int,int,int,Tuple<int>>
                                        (1, 2, 3, 4, 5, 6, 7, Tuple.Create(8));
var Item8 = t8.Rest.Item1; 

To create a tuple with more than 8 items, we do as follows:

C#
var t12 = new Tuple<int,int,int,int,int,int,int,Tuple<int,int,int,int, int>>
   	(1, 2, 3, 4, 5, 6, 7, new Tuple<int,int,int, int,int>(8,9,10, 11, 12));
	<>var Item10 = t12.Rest.Item3;

What Does A Tuple Represent?

Tuples do not have names that may have some significance. The attributes of a tuple are called "Item1", "Item2", and so on.

Two Tuples can be equal, but that doesn’t mean they are the same. Its meaning is not explicit, which can make your code less readable. For example, the following two tuples are equal, but represent different things:

(3, 9): Product Code 3 and Quantity 9
(3, 9): 3 and 9 are the codes of clients returned by a query.

As seen above, the fact that a tuple doesn’t carry information about its meaning, its use is generic and the developer decides what it will mean at the time of its creation and use.

So, Why Should We Use Them?

A) Return of Methods

Tuples provide a quick way to group multiple values into a single result, which can be very useful when used as a return of function, without the need to create parameters "ref" and / or "out ".

Example

C#
using System;
namespace TuplesConsoleTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var _cliente1 = RetornaCliente();
            Console.WriteLine("O código do usuário1 é: {0}", _cliente1.Item1);
            Console.WriteLine("O Nome do usuário1 é: {0}", _cliente1.Item2);
            Console.WriteLine("A data de nascimento do usuário1 é: {0}", 
                                        _cliente1.Item3.ToString("dd/MM/yyyy"));
            Console.Read();
        }
        static Tuple<int, string, DateTime> RetornaCliente()
        {
            Tuple<int, string, DateTime> _cliente = 
                        Tuple.Create(1, "Frederico", new DateTime(1975, 3, 24));
            return _cliente;
        }
    }
}

Another example of methods return is when we must return a list of an anonymous type. In this case, we can easily replace this type by tuples.

Example

C#
using System;
using System.Collections.Generic;
using System.Linq;
namespace TuplesConsoleTest
{
    class Program
    {
        static List<Tuple<int, string, string, DateTime>> lista;
        static void Main(string[] args)
        {
            CarregaLista();
            var result = SelecionaCLientes("M");
            foreach (var r in result)
            { 
                Console.WriteLine("Cliente: {0} \t Nome: {1}", r.Item1, r.Item2);
            }
            Console.Read();
        }
        private static void CarregaLista()
        {
            lista = new List<Tuple<int, string, string, DateTime>>();
            lista.Add(new Tuple<int, string, string, DateTime>
                            (0, "", "", DateTime.MinValue));
            lista.Add(new Tuple<int, string, string, DateTime>
                            (1, "Fred", "M", new DateTime(1975, 3, 24)));
            lista.Add(new Tuple<int, string, string, DateTime>
                            (2, "Rubia", "F", new DateTime(1983, 12, 17)));
            lista.Add(new Tuple<int, string, string, DateTime>
                            (3, "João", "M", new DateTime(2004, 4, 16)));
            lista.Add(new Tuple<int, string, string, DateTime>
                            (4, "Tatá", "F", new DateTime(1999, 7, 14)));
        }
        private static IEnumerable<Tuple<int, string>> SelecionaCLientes(string sex)
        {
            var ret = from t in lista
                      where t.Item3 == sex
                      select new Tuple<int, string>(t.Item1, t.Item2);
            return ret;
        }
    }
} 

B) Composite Key in a Dictionary

Due to the interface IEquatable defines GetHashCode(), the implementation of the interface IStructuralEquatable creates a Hash code combining the members Hash codes, allowing the use of tuples as a composite key for a collection of type Dictionary.

Example

C#
using System;
using System.Collections.Generic;
namespace TuplesConsoleTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var lista = ListaClienteConta();
            var chave = Tuple.Create(1, 1);
            Console.WriteLine("Saldo selecionado é: {0}", 
                        lista[chave].Saldo.ToString());

            Console.Read();
        }
        
        public static Dictionary<Tuple<int, int>, ClienteConta> ListaClienteConta()
        {
            Dictionary<Tuple<int, int>, ClienteConta> lista =
                                new Dictionary<Tuple<int, int>, ClienteConta>();
            ClienteConta cc1 = new ClienteConta(){
                Codigo_Cliente = 1,
                Codigo_Conta = 1,
                Saldo = 525.00 };
            ClienteConta cc2 = new ClienteConta(){
                Codigo_Cliente = 1,
                Codigo_Conta = 2,
                Saldo = 765.00 };
            lista.Add(Tuple.Create(cc1.Codigo_Cliente, cc1.Codigo_Conta), cc1);
            lista.Add(Tuple.Create(cc2.Codigo_Cliente, cc2.Codigo_Conta), cc2);
            return lista;
        }
    }
    public class ClienteConta
    {
        public int Codigo_Cliente { get; set; }
        public int Codigo_Conta { get; set; }
        public double Saldo { get; set; }
    }
} 

C) Replace Classes or Structs that are Created Just to Carry a Return or to Fill a List

Using the Tuple, we don’t need to create classes or structures to store only temporary values, such as creating a struct or class to add values to a combobox or listbox. With the tuples, it will no longer be necessary to create them.

Example

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TuplesTest
{
    public partial class Form1 : Form
    {
        List<Tuple<int, string>> lista = new List<Tuple<int, string>>();
        public Form1()
        {
            InitializeComponent();
            lista.Add(Tuple.Create(7, "Tânia"));
            lista.Add(Tuple.Create(2, "Rúbia"));
            lista.Add(Tuple.Create(4, "Haroldo"));
            lista.Add(Tuple.Create(1, "Frederico"));
            lista.Add(Tuple.Create(3, "João"));
            lista.Add(Tuple.Create(5, "Carlos"));
            lista.Add(Tuple.Create(6, "Samanta"));
            lista.Add(Tuple.Create(8, "Marcio"));
            lista.Add(Tuple.Create(9, "Carla"));
            lista.Add(Tuple.Create(10, "Francisco"));
        }
        private List<Tuple<int, string>> RetornaListaPessoasOrdenadaPorNome()
        {
            var lstOrdenada = lista.OrderBy(t => t.Item2).Select(t=>t).ToList();
            return lstOrdenada;
        }
        private List<Tuple<int, string>> RetornaListaPessoasOrdenadaPorCodigo()
        {
            var lstOrdenada = lista.OrderBy(t => t.Item1).Select(t => t).ToList();
            return lstOrdenada;
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            List<Tuple<int, string>> listaOrdenada;
            if (rbtNome.Checked)
                listaOrdenada = RetornaListaPessoasOrdenadaPorNome();
            else
                listaOrdenada = RetornaListaPessoasOrdenadaPorCodigo();
            lstNomes.DataSource = listaOrdenada;
            lstNomes.ValueMember = "Item1";
            lstNomes.DisplayMember = "Item2";
        }
    }
} 

form.JPG

Comparing and Ordering

The interfaces IStructuralComparable and IStructuralEquatable were introduced in .NET 4.0 to assist in supporting Tuples.

A tuple is equal to another if and only if all items are equal, i.e., t1.Item1 ==t2.Item1 and t1.Item2 == t2.Item2, and so on.

To sort, a comparison is made on individual items, i.e., the comparison is made in the first Item1 if t1.Item1> t2.Item1 then Tuple t2 is the smallest, if t1.Item1 == t2.Item1 then the comparison is made in item2 and so on.

To use the interfaces IComparable, IEquatable, IStructuralComparable and IStructuralEquatable, we must make the cast to the desired interface explicitly.
C#
Tuple<int, int> t1 = Tuple.Create(3, 9);
Tuple<int, int> t2 = Tuple.Create(3, 9);
Tuple<int, int> t3 = Tuple.Create(9, 3);
Tuple<int, int> t4 = Tuple.Create(9, 4);
 
Console.WriteLine("t1 = t2 : {0}", t1.Equals(t2)); //true
Console.WriteLine("t1 = t2 : {0}", t1 == t2); //false
Console.WriteLine("t1 = t3 : {0}", t1.Equals(t3)); // false
 
Console.WriteLine("t1 < t3 : {0}", ((IComparable)t1).CompareTo(t3) < 0); //true
Console.WriteLine("t3 < t4 : {0}", ((IComparable)t3).CompareTo(t4) < 0); //true

Conclusion

While the indiscriminated use of Tuples affects the readability of the code, its use at the appropriate time can be very handy for developers, allowing them to return multiple values from a function without the need to create parameters "ref" and / or "out", allowing the creation of composite keys to collections of type Dictionary and eliminates the need to create structs or classes or just to fill combobox or lists.

History

  • 9th May, 2011: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader ActionPoint
Ireland Ireland
I'm Brazilian currently living in in Limerick, Ireland. I've been working with software development since 1996.

Comments and Discussions

 
GeneralMy vote of 5 Pin
JIANGWilliam1-Mar-18 15:51
JIANGWilliam1-Mar-18 15:51 
QuestionMy vote of 5 Pin
Snesh Prajapati10-Nov-13 19:51
professionalSnesh Prajapati10-Nov-13 19:51 
GeneralFriends DONT let Friends use Tuples! Pin
rittjc11-May-13 13:35
rittjc11-May-13 13:35 
Great article and good introduction about Tuples. That is the only reason I give it a 3. Were Tuples actually useful and worth reading about, or had you mentioned they were useless and a bad idea, I would have given you a 5. But the article (for good reason) does not really tell you why you should a Tuple verses one of the other better alternatives. Why this is so important is that they are pretty much useless as far as I can tell and cannot figure out why anyone would want to use them.

Sure, its Gee-whiz, this is different, it looks cool, very "mathy", none of my friends use these, I gotta seem like a guru if I am using them, but other than that, I cannot understand why they were added to the language other than language purists from outdated or backward languages like F# perl, python, etc, don't like the amount of change they have.

For instance, you mention you can return an array of items without having an out or ref. Well, that can be done far easier and more readable and does not take arbitrary remembering of item meanings between providers and consumers, like the natural coordination with a structure and if you need fixed and typed parameters. You can't beat a structure for speed of creation and it stays off the heap (unless you box it).

Then there is the idea of quick and dirty for filling a combobox, list box, etc without having to declare a type. But, a KeyValuePair is much better at that because it can be easily bound and is more readable than Tuple declaration, not to mention it does not take up heap.

There is little info on the performance of Tuples especially as to the risk of unknown boxing which eats up time and heap. At a minimum, it is going to be allocated on the heap where a structure/KeyValuePair are value types allocated on the stack which takes zero time and do not need to be cleaned up by the GC.

Not only is there no obvious benefit other than to "confuse others who deal with your code", but just consider benefits of an array alone, has over a Tuple:

- An array is dynamically resizeable bigger or smaller without losing contents.
- An array can have both explicit and implicit typing.
- An array is less ambiguous. Indexing, at least to me, is clearer than referencing Item1, Item2, etc.
- An array can be multidimensional.
- An array can tell you its size.
- An array can be reversed, sorted, copied, appended, searched, dynamically tested and modified by simple predicates, comparers, etc.
- An array is clonable
- An array can be iterated with for loops
- An array inherits from IEnumberable to so it can be iterated with a foreach.
- An array can be passed as an abstract collection with ICollection and IList.
- An array can be serialized, tuples can't.

Its good writing. Decent examples. But as the title of my comment says "Friends don't let friends to use Tuples in C#!" WTF | WTF | :WTF:

Had you have put that Tuples are ill-advised and a bad coding practice because their poor readability and lack of benefit over existing types, in there, I would have definitely given you a 5!

BTW: I am glad there is a standard for C#, but I would like to say to those that believe languages should also be opened up, like C#, take note. When you open it up, design by committee gets you really dumb additions like this just because someone on the committee liked them in another language and suggested them and they were accepted to be politically correct and accommodating.

That's my take!
GeneralRe: Friends DONT let Friends use Tuples! Pin
Yuriy Loginov27-May-13 5:50
professionalYuriy Loginov27-May-13 5:50 
GeneralRe: Friends DONT let Friends use Tuples! Pin
rittjc27-May-13 19:17
rittjc27-May-13 19:17 
GeneralRe: Friends DONT let Friends use Tuples! Pin
Yuriy Loginov28-May-13 3:47
professionalYuriy Loginov28-May-13 3:47 
GeneralRe: Friends DONT let Friends use Tuples! Pin
rittjc28-May-13 12:13
rittjc28-May-13 12:13 
QuestionNice one! Pin
Sander Rossel29-Mar-13 21:15
professionalSander Rossel29-Mar-13 21:15 
GeneralMy vote of 5 Pin
Belial0916-Jan-13 21:32
Belial0916-Jan-13 21:32 
SuggestionIterate over two or more collections Pin
daflodedeing22-Nov-12 22:31
daflodedeing22-Nov-12 22:31 
Questiontuples Pin
arup200514-Jul-12 7:02
arup200514-Jul-12 7:02 
SuggestionMy vote of 5 Pin
Akram El Assas11-May-12 10:39
Akram El Assas11-May-12 10:39 
GeneralRe: My vote of 5 Pin
fmsalmeida11-May-12 11:39
professionalfmsalmeida11-May-12 11:39 
GeneralMy vote of 5 Pin
Dave Kerr12-Feb-12 12:11
mentorDave Kerr12-Feb-12 12:11 
GeneralMy vote of 5 Pin
DaveyM695-Dec-11 8:56
professionalDaveyM695-Dec-11 8:56 
Question+5 and a question ... Pin
BillWoodruff5-Dec-11 7:00
professionalBillWoodruff5-Dec-11 7:00 
Question'unreadable' is a little harsh Pin
dave.dolan19-Jun-11 18:11
dave.dolan19-Jun-11 18:11 
AnswerRe: 'unreadable' is a little harsh Pin
fmsalmeida20-Jun-11 2:38
professionalfmsalmeida20-Jun-11 2:38 
GeneralMy vote of 5 Pin
Jonathan Cardy19-Jun-11 1:14
Jonathan Cardy19-Jun-11 1:14 
GeneralRe: My vote of 5 Pin
fmsalmeida20-Jun-11 2:37
professionalfmsalmeida20-Jun-11 2:37 
GeneralMy vote of 5 Pin
Monjurul Habib17-Jun-11 10:39
professionalMonjurul Habib17-Jun-11 10:39 
GeneralRe: My vote of 5 Pin
fmsalmeida20-Jun-11 2:36
professionalfmsalmeida20-Jun-11 2:36 
GeneralMy vote of 5 Pin
Andy Missico15-Jun-11 9:17
Andy Missico15-Jun-11 9:17 
GeneralRe: My vote of 5 Pin
fmsalmeida20-Jun-11 2:36
professionalfmsalmeida20-Jun-11 2:36 
Generalnice Pin
BillW3313-Jun-11 4:19
professionalBillW3313-Jun-11 4:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.