Subscribe - It's FREE!!

Stay Connected Here

Stay Updated With Us Here



Google

What is Tuple in C#? How and when to use it?


Share with WhatsApp


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. 
    E.g.
    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.

  1. If you want to return the multiple values to be returned from the method (and don't want to define a class)
  2. You are having small set of data which can be easily represented in Tuple (avoid using Tuple with more values)
  3. 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.



If you enjoyed this post take 5 seconds to share it! Be Socialable. :-)

Share with WhatsApp

Posts To Read Next

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.


Visual Studio 2008 Shell and TFS integration

Visual Studio 2008 Shell and TFS integration is the problem for all newbies of BIDS and TFS. Here is the solution.


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.


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.


Speed up coding in Visual Studio with code snippets & samples at your fingertips

Know how you can speed up coding in Visual Studio with Bing Developer Assistant by having millions of code snippets and sample projects at fingertips.


Your opinion is valuable for us! Comments, suggetions are welcome.


Submit your Email Id to stay updated with us and get notified with our new posts. It's FREE!
We know this popup is disturbing you!
But We would greatly appreciate if you share us with your friends below!

It will not take more than 2 seconds but will motivate us greatly to write more,share more!

x