Lately I was taking some developers interview and one of the question that I asked them was “How they can return multiple values of different type from a method“. Most of them came up with the answer of “ref” and “out” keyword, some also said they will wrap the result to a temporary object and they will return that object to the caller. While all of these answers are technically correct (although ref/out not works with async method), I realize that most of the developers are not aware of Tuple, a data structure that were introduced back on .Net 4.0
I talked with some of my colleague and they said they found it ridiculously bulky. Yes, It was little bit bulky, BUT NOT ANYMORE with the release of C# 7.0
I will show the new lightweight syntax at the the end of this post. Feel free to jump on that section directly if you are already familiar with what Tuple actually is, how it works and so on.
Here goes the code snippet:
As you can see from the code snippet the method OldWay_UserInfo returns a Tuple that ultimately returns two string and a int value. You can create Tuple in two ways. Using constructors for your type’s corresponding argument (line 6, TupleCreation_1) or using the static Tuple.Create method (line 8, TupleCreation_1).
Retrieving the result of Tuple is done by accessing the read only property (Item1, Item2, Item3, … Item8).
A tuple is an ordered, immutable, fixed size of heterogeneous objects. That mean the order of items strictly follows the order used at the time of its creation. Plus the size of the tuple was set at time of its creation. You can not add or remove items on the fly.
A single tuples could have a max 8 items. If I ever need to pass more then that I can always wrap the extra information inside a new tuple and pass that as the last argument (TRest) of my first tuple i.e. If you want to create a tuple with more items, you have to go for nested Tuples ( Tuple<T1, T2, …, TRest>). In fact, you could even put a third tuple inside the second and so on.
Accessing the last element (8th element) is little ticker. I have to call the Rest property.
Tuple gets better with C# 7.0
With the release of C# 7.0, the usages of Tuple gets simpler then ever. I will re-write the first tuple example with the latest syntax. Lets not waste any time. Here is the new elegant snippet :
Don’t sweat to understand the underlying details. To me, it feels like, its basically syntactical sugar on top of the existing work. I can still access data with the read only property (Item1, Item2, Item3, … Item8) like I did before.
Calling Item1, Item2, …. etc does not make much sense often. Simply because these names aren’t very descriptive. Sometime you might want to call them with more meaningful name, with the latest release you can achieve it like this:
This approach is good when I have zero or few params in the method signature. If I have good number of param in the method signature then I prefer to use the below style:
Here I am specifying element names directly in tuple literals (line 6, TupleCreation_5)
Tuples rely on a set of underlying types which aren’t included in Visual Studio “15” Preview 4. Current work around is to install the nuget package System.ValueTuple
Tuples can be very handy for developers, you can just return a Tuple and have a nice method signature without having to create a new POCO.