Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Tuple in C# 4.0

0.00/5 (No votes)
5 Oct 2009 1  
C# 4.0 includes a new feature called Tuple.

C# 4.0 includes a new feature called Tuple. In mathematics, tuple is an ordered list of specific number of values called the components of the tuple. For example, a 3-tuple name may be used as: (First-Name, Middle-Name, Last-Name).

Let's take a look at the following example:

public Tuple<int, int> GetDivAndRemainder(int i, int j) 
{ 
    Tuple.Create(i/j, i%j); 
} 
public void CallMethod() 
{ 
    var tuple = GetDivAndRemainder(10,3); 
    Console.WriteLine("{0} and {1}", tuple.item1, tuple.item2); 
} 

In the above example, the method can return a tuple which has two integer values. So using tuple, we can return multiple values. So this will help lazy programmers to write less code but do more. One great use of tuple might be returning multiple values from a method.

To get more information on Tuple, visit the following links:

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here