Subscribe - It's FREE!!

Stay Connected Here

Stay Updated With Us Here


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

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.

  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

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.

What is Hoisting in JavaScript?

Hoisting in JavaScript, one of the most misunderstood or little understood concept in the JavaScript and remain famous as interview question for JavaScript developers and now also for angular js developer. Lets check out what it means exactly.

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.

How to post JSON string as a string in jQuery AJAX

Know how to post JSON String as string to the action method of controller in MVC.

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.

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!