Click here to Skip to main content
15,881,852 members
Articles / Programming Languages / C#

Tuple in C# 4.0

Rate me:
Please Sign up or sign in to vote.
3.89/5 (16 votes)
11 Aug 2010CPOL 41.2K   14   11
This article explains the new data type “Tuple” introduced by framework 4.0.

What is a Tuple?

In mathematics, A Tuple is a sequence of finite length. An n-tuple is a tuple with n elements. For example (2, 4, 3, 8) is a 4-tuple.

C# 4.0 introduced a new data type Tuple. Tuple is not new in software engineering, but of course it is new in .NET framework 4.0. A tuple is a simple generic data structure that holds an ordered set of items of heterogeneous types. There are two ways to instantiate Tuple by calling either the Tuple constructor or the static Tuple.Create() method.

C#
// Instantiate using constructor
Tuple<int, string, int> t1 = new Tuple<int, string, int>(3, "Frank", 9);

// Instantiate using create method
Tuple<int, int, int> t2 = Tuple.Create(3, 5, 9);

Tuple constructor and create() method can contain a maximum of 8 parameters. You have to take care of the 8th parameter because it replaces another tuple object.

Simple Use of Tuple

You can easily return more than one value from method without using out or ref parameters.

C#
public Tuple<int, int> SplitPoints(string point)
{
   string[] pointList = point.Split(',');

   int x = Convert.ToInt32(pointList[0]);
   int y = Convert.ToInt32(pointList[1]);
   return Tuple.Create<int, int>(x, y);
 }

SplitPoints method split the points and returns x and y points in tuple.

C#
Tuple<int,int> points = SplitPoints("12,14");
string msg = string.Format("X: {0}, Y: {1}", points.Item1, points.Item2);
MessageBox.Show(msg);

You can get the returned values from exposed properties Item1, Item2, etc. by Tuple object. Item properties of Tuple object are readonly. You can not change the value of the property.

This article was originally posted at http://shakeel-dot-net.blogspot.com/feeds/posts/default

License

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


Written By
Chief Technology Officer
Pakistan Pakistan
Passion and positive dedication is essential part of success. I believe on hardworking and sharing knowledge with others. I always try to be a better than I am and think positive for positive result.

My Blogs

My Linked-In Profile

Comments and Discussions

 
GeneralGreat post! Pin
Priyanka Kale7-Mar-18 22:22
Priyanka Kale7-Mar-18 22:22 
GeneralMy vote of 4 Pin
sawant parag22-Mar-13 23:15
sawant parag22-Mar-13 23:15 
GeneralMy vote of 3 Pin
Nicolas Dorier16-Feb-12 1:08
professionalNicolas Dorier16-Feb-12 1:08 
Generalgood one Pin
pintoevita16-Aug-10 23:16
pintoevita16-Aug-10 23:16 
GeneralMy vote of 4 Pin
GPUToaster™11-Aug-10 23:37
GPUToaster™11-Aug-10 23:37 
GeneralMy vote of 5 Pin
Kim Togo11-Aug-10 0:56
professionalKim Togo11-Aug-10 0:56 
GeneralMy vote of 5 Pin
seer_tenedos24-Aug-10 12:46
seer_tenedos24-Aug-10 12:46 
GeneralRe: My vote of 5 Pin
Ed Nutting10-May-11 9:52
Ed Nutting10-May-11 9:52 
GeneralExample code. Pin
jlafay4-Aug-10 5:43
jlafay4-Aug-10 5:43 
GeneralRe: Example code. Pin
Shakeel Iqbal4-Aug-10 6:04
Shakeel Iqbal4-Aug-10 6:04 
GeneralRe: Example code. Pin
Richard Deeming16-Aug-10 8:55
mveRichard Deeming16-Aug-10 8:55 

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.