- Posted On 26 March 2015
- In Programming
This post covers information about Tuple in C#. Many of the experienced dot net developers are still not aware about tuple as it was introduced in C# 4.0 and it is now one of the favorite interview question (dont know why :-)) so I decided to share about it here so the developers like you reading my post will have some information about it in their knowledge bank.
What is Tuple?
A tuple is a data structure that has a specific number and sequence of elements. Let see it by example, you can have a data structure of the 3 elements for the geographical information such as State,City and Pincode like following in Tuple.
var geo = new Tuple<string, string, int>("Maharashtra","Pune",411003);
So now in above tuple we have stored information in Tuple where first element is State, second element is City and third element is Pincode. So to access this information we can have a code like following.
geo.Item1; //state geo.Item2; //city geo.Item3;//pincode
You can also use "Create" method to define a tuple like following.
var geo =Tuple.Create("Maharashtra","Pune",411003);
More about Tuple.
- Tuple itself doesnt represent a tuple, it's a static factory class which provides static methods for creating instances of tuple types.
- Tuple Types are reference types.
- The .NET Framework directly supports tuples with one to seven elements, for further more you need to pass the tuple type in to 8th element which is a rest property.
var numbers = Tuple.Create(1,2,3,4,5,6,7,Tuple.Create(8,9,10));
When to use Tuple?
Main problem in using tuple is it makes code difficult to read as it uses sequences to read the values such as Item1, Item2 and so on , due to which caller or reader need to check the declaration or comment(if available) to know the sequence of each value.,but still it can be used in following cases.
- If you want to return the multiple values to be returned from the method (and don't want to define a class)
- You are having small set of data which can be easily represented in Tuple (avoid using Tuple with more values)
- Passing multple (again not much) parameters to the method without defining a class.
I hope I am able to explore the Tuple in C#, if you are having something positive to add do share it in comment section below. Thanks.
Swagger - Powerful Opensource Framework for RESTful API
With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability. Swagger Editor allows you to generate code, documentation with ease.
Visual Studio Code - Lighweight but powerful source code editor available for Windows, OS X and Linux
Assembla - Free and private repository to manage your source code online with SVN subversion hosting
With Assembla you can share source code with others online. Free & Private source code repository with SVN Subversion, Git & Perforce Hosting.
Top 10 Visual Studio things which can boost developers coding speed
Visual Studio 2012 provides some coding features by which you can code faster if use them properly. This post will cover top 10 things among them to boost your development speed.
How to call click or any event only once in jQuery
Know how to execute an click event or any event only once for any element in jQuery. Perform action only once and even not required to unbind event.