Introduction to the Tuple in C# 4.0

No.of Views2596
Bookmarked0 times
Downloads 
Votes0
By  abhi2434   On  15 Nov 2010 10:11:06
Tag : CSharp , CSharp4.0
Through this article, i will introduce in FCL named Tuple which can store n - number of values in it. Yes, you specify the type of each of those variables as generic parameters, and the object will create those values for you
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this article,I will write about a very handy object introduced in FCL named Tuple which can store n - number of values in it.  Yes, you specify the type of each of those variables as generic parameters, and the object will create those values for you. Lets see how it works.

Using a Tuple

Base class library exposes two objects. It exposes the static class Tuple which allows you to get a number of Tuple instances based on the Static method Create, and a number of Tuple classes, each of which takes some specific number of Generic arguments.

  • 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>

 Tuple.Create has 8 overloads and each of these overloads returns new object of Tuple<t1, t2...t8> class.  So .NET framework exposes 8 different classes each of them taking T1..... T8 number of arguments and each of them exposes Item1..... Item8 number of arguments.

The Tuple class can be instantiated directly as well without using static objects. Even If you see in Reflector, all the Create method actually returns its respective Tuple object.  Hence, lets create object of Tuple.

Tuple<int, string> tuple = new Tuple<int, string>(20, "Abhishek")

 Here the class with two Generic Type argument is created and hence it exposes the items as Item1 and Item2.

 

 

Image Loading

Similar to this, you can also create Tuple of 3, 4, 5 .....7 types 
How to generate N number of argument list?
Tuple actually supports more than 8 argument as it expects the 8th argument as another Tuple. Say for instance, if you write :

Tuple<int, string,int,int,int,int,int,int> tuple = new Tuple<int, string,int,int,int,int,int,int>(20, "Abhishek", 39, 39,59,49,30, 33);

 You will eventually end up with an ArgumentException.

 

Image Loading

Hence the appropriate call to it must be :

Tuple<int, string,int,int,int,int,int,tuple<int>> tuple = new Tuple<int, string,int,int,int,int,int,tuple<int>>(20, "Abhishek", 39, 39,59,49,30, new Tuple<int>(33));

 So you can see the last generic argument we pass as Tuple. Using this argument you can create as many arguments as you want.

Is Tuple a collection ?

it must be an interesting question. But its not. Generally we call an object to be a collection only when all the elements inside the object are of same Type. In case of Tuple, each type might differ based on the Type argument, hence its not a collection.

Know a bit more

For enthusiastic, let me put this a bit further. After working for a while with Tuple, I was thinking why didnt microsoft expose an interface to define each of these Tuple classes (say ITuple) and Restrict the last argument TRest (8th argument)  using Generic constraint? To see what was the problem, I tried to dissemble it and seen few strange designs.

  1. Each Tuple is implemented from an Internal interface ITuple.
  2. ITuple cannot be accessed from outside and is kept hidden.
  3. Generic Constraint is not used for TRest while it throws ArgumentException intentionally from the constructor.

 

Image Loading

Interesting to Know

It should be noted, ToString is been implemented very well with Tuple objects. It puts the value as comma separated string with first braces around it.

Tuple<int, string,int,int,int,int,int,Tuple<int>> tuple = new Tuple<int, string,int,int,int,int,int,Tuple<int>>(20, "Abhishek", 39, 39,59,49,30, new Tuple<int>(33));
Console.WriteLine(tuple);

It will actually print :

(20, Abhishek, 39, 39, 59, 49, 30, 33)

Cool huh. I hope you would use Tuple in your code very often.

Conclusion

Through this article, i will introduced in FCL named Tuple which can store n - number of values in it.Thank you for reading.

 
Sign Up to vote for this article
 
About Author
 
abhi2434
Occupation-Not Provided
Company-Not Provided
Member Type-Senior
Location-Not Provided
Joined date-22 Oct 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
    Since we have multicore processors are now so we can take advantages of multicore processor with parallel execution in C# 4.0. There are some time consuming task for the computer for example a long for loop or similar kind of things. This kind of task can be done parallel with parallel class in C# 4.0.
    Published Date : 12/Jul/2011
    In this article, i will explain how to use the How to use ExpandoObject Class in .NET 4.0.The ExpandoObject class is introduced on .NET Framework 4.0 and inherited using many interfaces.
    Published Date : 16/Apr/2011
    C# 4.0 supports Dynamic Programming by introducing new Dynamic Typed Objects
    Published Date : 26/May/2010
    Dynamic Datatype in C# 4.0 is quit bit familiar to Var Datatype. Deference between Var and Dynamic is VAR initialized on CompileTime and Dynamic initialized On Runtime
    Published Date : 17/May/2010
    In this article I will show how to use the IObserver and IObservable Interfaces in C#. These two interfaces is work with connection to Push based approach on Reactive Framework
    Published Date : 15/Dec/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top