Tuple in C# 4.0

Posted: November 29, 2009 in .NET, C#
Tags: ,

Tuple provides us with a way to group elements of disparate data types together.This is present in functional languages like Haskell and also dynamic languages like Python.A common example of a tuple is a pair of coordinates defining a point in two dimensional space.In Haskell a tuple storing name and age of person is defined as :


let t = ("Bob",57)

In Python it will look something like:

t = "Bob",57

In C# 4.0 we have a provision to create tuples but the syntax is not as beautiful as it is in Haskell and Python.We have got a set of Tuple classes as shown below which helps us to work with tuples:

  • Tuple – This static class provides 8 overloads of Create methods to create tuples of size 1 to 8
    • Create(T1)
    • Create(T1,T2)
    • Create(T1,T2,T3)
    • Create(T1,T2,T3,T4)
    • Create(T1,T2,T3,T4,T5)
    • Create(T1,T2,T3,T4,T5,T6)
    • Create(T1,T2,T3,T4,T5,T6,T7)
    • Create(T1,T2,T3,T4,T5,T6,T7,T8)
  • Tuple<T1> – Represents a tuple of size 1
  • Tuple<T1,T2> – Represents a tuple of size 2
  • Tuple<T1,T2,T3> – Represents a tuple of size 3
  • Tuple<T1,T2,T3,T4> – Represents a tuple of size 4
  • Tuple<T1,T2,T3,T4,T5> – Represents a tuple of size 5
  • Tuple<T1,T2,T3,T4,T5,T6> – Represents a tuple of size 6
  • Tuple<T1,T2,T3,T4,T5,T6,T7> – Represents a tuple of size 7
  • Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> – Represents a tuple of size 8 or greater.

Consider the following code:

static void Main(string[] args)
{
    var t = Tuple.Create<string, int>("Bob", 57);
    Console.WriteLine(t);
    Console.Read();
} 

It creates a tuple same as what we saw in Haskell/Python samples and the program prints:

(Bob, 57)

We can create nested tuples as shown below:

static void Main(string[] args)
{
    var t = Tuple.Create("Bob", 57,new Tuple<string,string>("California","Los Angeles"));
    Console.WriteLine(t);
    Console.Read();
}  

The program will print out:

(Bob, 57, (California, Los Angeles))

The class Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> creates a Tuple with 8 or more elements whereas method Create(T1,T2,T3,T4,T5,T6,T8)creates a tuple with eight elements.This is clear from the example shown below:

static void Main(string[] args)
{ 

    var t1 = Tuple.Create("Test", 1, 2, 3, 4, 5, 6, 
                          new Tuple<int, int>(7, 8));
    Console.WriteLine("Nested Tuple with 8 elements" + t1); 

    var t2 = new Tuple<string,int,int,int,
                       int,int,int,Tuple<int,int>>
                       ("Test", 1, 2, 3, 4, 5, 6, 
                       new Tuple<int, int>(7, 8)); 

    Console.WriteLine("Tuple with 9 elements" + t2); 

    Console.Read();
} 

The program prints out:

Nested Tuple with 8 elements(Test, 1, 2, 3, 4, 5, 6, (7, 8))
Tuple with 9 elements(Test, 1, 2, 3, 4, 5, 6, 7, 8)

The values stored inside a tuple can be accessed using the ItemN properties as shown below:

static void Main(string[] args)
{
    var t = Tuple.Create("Bob", 57);
    Console.WriteLine("Name:{0},Age:{1}",t.Item1,t.Item2);
    Console.Read();
} 

Here we do not have enumerator to iterate through this because this is not a collection.This is more closer to anonymous class with anonymous properties as well.In pure functional languages like Haskell this can be very helpful in returning multiple values of disparate types from a function.But how come in a language like C# or even python this can be helpful where we have got classes.The answer is, sometimes we create a class just to store some temporary data for programming purpose where the class does not have any meaning.A tuple can be very helpful in such a situation.

About these ads
Comments
  1. Tuple in C# 4.0 « Sankarsan’s Journal…

    Thank you for submitting this cool story – Trackback from DotNetShoutout…

  2. Tuple in C# 4.0 « Sankarsan’s Journal…

    Thank you for submitting this cool story – Trackback from PimpThisBlog.com…

  3. Tuple in C# 4.0 « Sankarsan’s Journal…

    Thank you for submitting this cool story – Trackback from Servefault.com…

  4. progg.ru says:

    Tuple в C# 4.0…

    Thank you for submitting this cool story – Trackback from progg.ru…

  5. [...] This post was mentioned on Twitter by progg, Sankarsan Bose. Sankarsan Bose said: RT @sankarsan Tuple in C# 4.0 – Sankarsan’s Journal http://bit.ly/4DG6E7 [...]

  6. Richard says:

    Presumably “Create(T1,T2,T3,T4,T5,T6,T8)” should be “Create(T1,T2,T3,T4,T5,T6,T7,T8)”?

  7. Great article, thank you for writing about this. You have a lot of educational articles here, thanks again! I found a brief primer on Software Testing, do you think it is any good? I’m curious about such introductory articles for someone who is thinking about getting into Testing. Click here if you’d like to check out my site.

  8. Rahul says:

    First of all. Thanks very much for your useful post.

    I just came across your blog and wanted to drop you a note telling you how impressed I was with the

    information you have posted here.

    Please let me introduce you some info related to this post and I hope that it is useful for community.

    There is a good C# resource site, Have alook

    http://CSharpTalk.com

    Thanks again
    Rahul

  9. Ecko says:

    thanks for sharing,,,

  10. dimitree says:

    Hello

    For var whether we need to add any namespace , i searched in tht they given LINQ namespace to be added after adding tht also error is coming what to do pls help me

    Thanks

  11. Caio says:

    Nice! Tuple collection is very interesting.

    when I create:

    public class table1
    {
    public table1() { }

    public int ID {get; set;}
    public string NAME {get; set; }
    }

    and a List give me the same resources for use. Am I right?

    What advantages i got to use tuple?

    thanks?

  12. [...] Tuple in C# 4.0 « Sankarsan’s Journal (tags: c# tuple dynmic python) [...]

  13. njappboy says:

    Caio, you’d use the Tuple instead of creating a throw-away class like table1, when table1 doesn’t have any usage outside of some narrow scope

  14. Blogs ou should be reading…

    [...]Here is a Great Blog You Might Find Interesting that we Encourage You[...]……

  15. [...] In C#, it is a simple play of generics [...]

  16. Ganesh says:

    Why cant I declare a Tuple like this

    Tuple<int, int, int, int, int, int, int, Tuple> a = Tuple.Create(1, 1, 1, 1, 1, 1, 1, new Tuple(1, 1));

    It gives me a error
    Cannot implicitly convert type ‘System.Tuple<int,int,int,int,int,int,int,System.Tuple<System.Tuple>>’ to ‘System.Tuple<int,int,int,int,int,int,int,System.Tuple>’

    However the following statements works fine
    Tuple<int,Tuple> a = Tuple.Create(1, new Tuple(1, 1));

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s