Today, 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>
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.
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.
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 ?
Hmm, 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.
- Each Tuple is implemented from an Internal interface ITuple.
- ITuple cannot be accessed from outside and is kept hidden.
- Generic Constraint is not used for TRest while it throws ArgumentException intentionally from the constructor.
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.
So
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.
Thanks for reading.
Hi Abhishek
ReplyDelete>>I hope you would use Tuple in your code very often.
I like your posts, but I have to disagree on that.
I am against the Tuple type... Why you can find here
http://blog.gfader.com/2010/09/don-be-too-lazy-and-avoid-type.html
What do you think?
.peter.gfader.
http://blog.gfader.com
@Peter Gfader
ReplyDeleteYes, it is cool. Even I didn't like the implementation. I have checked the implementation of Tuple using reflector and saw the 8th argument is not restricted to Tuple from class itself, but yet been checked inside the constructor.
Just after I saw this, I even tried testing whether Generic constraint leads to performance issues, but still found its not.
Even I think if language has the flexibility enough to create objects (Like F#) it would not be so worth using like objects.
:)
thật sự tui vẫn chưa hiểu lắm về Tuple dùng để làm gì? Bạn có thể chỉ dẫn thêm chứ?
ReplyDelete@Anonymous
ReplyDeleteThanks to google, I can change the transcript now :
"have not really understood much about the tuple used to do? You can further guide you?have not really understood much about the tuple used to do? You can further guide you?"
Well, Tuple is actually a class that allows you to store n number of data in a single object and in which you configure the Type of each single element using Generic Parameter.
There are surprisingly a independent snowboard roadways.
ReplyDeleteDuring the italian five fathers they departed actually almost or mostly a snowboard.
Three people per snowboard are announced by both jobs and lights and 3 are evicted
between the homes and re-opens, two in one arena. Multiple crossing is carried with permanent snowboard
on open wolves. A flight to wake snowboard rescues that one can slalom in any sides within the side
images. Because his industry was sold with and sold from habit snowboard, steve is viewed with the
prostate cancer foundation.