Click here to Skip to main content
11,927,711 members (50,780 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


14 bookmarked

Tuple in C# 4.0

, 11 Aug 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

 // 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.

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.

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

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Shakeel Iqbal
Software Developer (Senior) TEO
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

You may also be interested in...

Comments and Discussions

GeneralMy vote of 4 Pin
sawant parag23-Mar-13 0:15
membersawant parag23-Mar-13 0:15 
GeneralMy vote of 3 Pin
Nicolas Dorier16-Feb-12 2:08
memberNicolas Dorier16-Feb-12 2:08 
Generalgood one Pin
pintoevita17-Aug-10 0:16
memberpintoevita17-Aug-10 0:16 
GeneralMy vote of 4 Pin
GPUToaster12-Aug-10 0:37
memberGPUToaster12-Aug-10 0:37 
GeneralMy vote of 5 Pin
Kim Togo11-Aug-10 1:56
memberKim Togo11-Aug-10 1:56 
GeneralMy vote of 5 Pin
seer_tenedos24-Aug-10 13:46
memberseer_tenedos24-Aug-10 13:46 
GeneralRe: My vote of 5 Pin
EdMan19610-May-11 10:52
memberEdMan19610-May-11 10:52 
GeneralExample code. Pin
jlafay4-Aug-10 6:43
memberjlafay4-Aug-10 6:43 
GeneralRe: Example code. Pin
Shakeel Iqbal4-Aug-10 7:04
memberShakeel Iqbal4-Aug-10 7:04 
GeneralRe: Example code. Pin
Richard Deeming16-Aug-10 9:55
memberRichard Deeming16-Aug-10 9: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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 12 Aug 2010
Article Copyright 2010 by Shakeel Iqbal
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid