Click here to Skip to main content
15,887,596 members
Articles / Programming Languages / C#

Tuples in C#

Rate me:
Please Sign up or sign in to vote.
3.85/5 (23 votes)
22 Mar 2015CPOL2 min read 27.6K   12   10
Tuples in C#

Introduction

Any function we use in C# can return maximum of single value which can be string, int or any other type, as per our requirements. If more than one value is to be returned, we use different types of approaches, like using anyDictionary with key-value pair, or any new container class, with properties of this class being the data/parameters we would like to be returned from that function or use the out or ref based parameters. All these are good if they are fulfilling our requirements. But C# provides a Tuple class, which can be used as a more efficient way of returning multiple values from a function.

Tuples were introduced in C# 4.0. In simple terms, you can define a tuple as a single record of different types of data. For example, a single record with UserId, FirstName, LastName, Salary, etc. can be called as a tuple. It's not necessary that all these attributes are of the same user. UserId can be of one user, FirstName could be of another user, Salary could be of the third user and so on.

Enough of theory, let's do some practical. There are two different ways how a tuple can be created. First is simply creating a new instance of the Tuple class and explicitly specifying the types of elements this tuple can have. The values to be stored in this tuple are passed in its parameterized constructor. See the code below:

Image 1

Accessing the parameters is very easy. Just use _tuple.Item1, tuple.Item2 where Item1 and Item2 are the elements at the first and second position of the tuple. See the code below:

Image 2

So the complete code becomes:

Image 3

Another way to create a tuple is use the Create method of the tuple class. However, the method to access the elements remains the same. See the code below:

Image 4

So these were two different ways of creating a tuple. An important point, there is a limit of 7 elements that a tuple can hold. The eighth element can only be another tuple. Although if you try to declare an eighth element of any type other than a tuple type, there will be no compile time error. But it will break at run-time with the error:

The last element of an eight element tuple must be a Tuple.

So if you need to have more than 7 elements in a tuple, you can use the eighth placeholder as a nested tuple. To access its element, you have a property named Rest, which further contains the elements of the nested tuple as Item1, Item2, etc. See the code below:

Image 5

So this was about the concept of tuples. Hope it helps you. Happy coding...!!!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
.Net developer and blogger by profession, keen to learn new technologies, love nature, music and cricket.

Blogger@http://dotnetfreakblog.wordpress.com

Comments and Discussions

 
QuestionDidn't address the core point. Pin
Libiom23-Mar-15 10:07
Libiom23-Mar-15 10:07 
Questionnice Pin
BillW3323-Mar-15 4:47
professionalBillW3323-Mar-15 4:47 
This is a nice blog entry, as far as it goes. I would like to see more in depth coverage of the topic though.
Just because the code works, it doesn't mean that it is good code.

GeneralMy vote of 4 Pin
cjb11022-Mar-15 22:31
cjb11022-Mar-15 22:31 
GeneralRe: My vote of 4 Pin
Carlos190723-Mar-15 0:48
professionalCarlos190723-Mar-15 0:48 
GeneralRe: My vote of 4 Pin
Member 768261623-Mar-15 3:45
Member 768261623-Mar-15 3:45 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun22-Mar-15 21:44
Humayun Kabir Mamun22-Mar-15 21:44 
QuestionHarly an article Pin
phil.o22-Mar-15 12:45
professionalphil.o22-Mar-15 12:45 
AnswerRe: Harly an article Pin
BillW3323-Mar-15 3:22
professionalBillW3323-Mar-15 3:22 
SuggestionTuple.Create doesn't require to specify argument types Pin
Aurimas22-Mar-15 11:11
Aurimas22-Mar-15 11:11 
GeneralLittle Article about Tuples Pin
macika12322-Mar-15 11:03
macika12322-Mar-15 11:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.