Click here to Skip to main content
Licence CPOL
First Posted 4 Aug 2010
Views 14,757
Bookmarked 13 times

Tuple in C# 4.0

By | 11 Aug 2010 | Technical Blog
This article explains the new data type “Tuple” introduced by framework 4.0.
A Technical Blog article. View original blog here.[^]

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);
 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.

License

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

Member

i have been working in software house as senior software engineer. i have worked on many web and windows projects. My hobbies are to read books and develop my own small utilities.
 

My Blogs
Follow me on Twitter

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 3 PinmemberNicolas Dorier1:08 16 Feb '12  
Generalgood one Pinmemberpintoevita23:16 16 Aug '10  
GeneralMy vote of 4 PinmemberGPUToaster23:37 11 Aug '10  
GeneralMy vote of 5 PinmemberKim Togo0:56 11 Aug '10  
GeneralMy vote of 5 Pinmemberseer_tenedos212:46 4 Aug '10  
GeneralRe: My vote of 5 PinmemberEdMan1969:52 10 May '11  
GeneralExample code. Pinmemberjlafay5:43 4 Aug '10  
GeneralRe: Example code. PinmemberShakeel Iqbal6:04 4 Aug '10  
GeneralRe: Example code. PinmemberRichard Deeming8:55 16 Aug '10  

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

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

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