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

Typewriter - Automatic TypeScript Template generation from C# classes in Visual Studio

Typewriter, an extension for visual studio that generates TypeScript files from C# code files using TypeScript templates. Convert your C# model classes to TypeScript automatically.


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

Visual Studio Code is a lightweight but powerful source code editor which runs on your desktop and is available for Windows, OS X and Linux. It comes with built-in support for JavaScript, TypeScript and Node.js and has a rich ecosystem of extensions for other languages (C++, C#, Python, PHP) and runtimes.


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.


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!
Vu 32'' TV Vu 24'' TV
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