8 out of 21 rated this helpful - Rate this topic

Tuple Class

Updated: June 2011

Provides static methods for creating tuple objects.

System.Object
  System.Tuple

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public static class Tuple
  Name Description
Public method Static member Supported by Portable Class Library Create<T1>(T1) Creates a new 1-tuple, or singleton.
Public method Static member Supported by Portable Class Library Create<T1, T2>(T1, T2) Creates a new 2-tuple, or pair.
Public method Static member Supported by Portable Class Library Create<T1, T2, T3>(T1, T2, T3) Creates a new 3-tuple, or triple.
Public method Static member Supported by Portable Class Library Create<T1, T2, T3, T4>(T1, T2, T3, T4) Creates a new 4-tuple, or quadruple.
Public method Static member Supported by Portable Class Library Create<T1, T2, T3, T4, T5>(T1, T2, T3, T4, T5) Creates a new 5-tuple, or quintuple.
Public method Static member Supported by Portable Class Library Create<T1, T2, T3, T4, T5, T6>(T1, T2, T3, T4, T5, T6) Creates a new 6-tuple, or sextuple.
Public method Static member Supported by Portable Class Library Create<T1, T2, T3, T4, T5, T6, T7>(T1, T2, T3, T4, T5, T6, T7) Creates a new 7-tuple, or septuple.
Public method Static member Supported by Portable Class Library Create<T1, T2, T3, T4, T5, T6, T7, T8>(T1, T2, T3, T4, T5, T6, T7, T8) Creates a new 8-tuple, or octuple.
Top

A tuple is a data structure that has a specific number and sequence of elements. An example of a tuple is a data structure with three elements (known as a 3-tuple or triple) that is used to store an identifier such as a person's name in the first element, a year in the second element, and the person's income for that year in the third element. The .NET Framework directly supports tuples with one to seven elements. In addition, you can create tuples of eight or more elements by nesting tuple objects in the Rest property of a Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> object.

Tuples are commonly used in four ways:

  • To represent a single set of data. For example, a tuple can represent a database record, and its components can represent individual fields of the record.

  • To provide easy access to, and manipulation of, a data set.

  • To return multiple values from a method without using out parameters (in C#) or ByRef parameters (in Visual Basic).

  • To pass multiple values to a method through a single parameter. For example, the Thread.Start(Object) method has a single parameter that lets you supply one value to the method that the thread executes at startup time. If you supply a Tuple<T1, T2, T3> object as the method argument, you can supply the thread’s startup routine with three items of data.

The Tuple class does not itself represent a tuple. Instead, it is a factory class that provides static methods for creating instances of the tuple types that are supported by the .NET Framework. It provides helper methods that you can call to instantiate tuple objects without having to explicitly specify the type of each tuple component.

Note Note

For information about the factory class pattern, see Best Practices: Seeing Patterns: The Factory Method.

Although you can create an instance of a tuple class by calling its class constructor, the code to do so can be cumbersome. The following example uses a class constructor to create a 7-tuple or septuple that contains population data for New York City for each census from 1950 through 2000.


var population = new Tuple<string, int, int, int, int, int, int>(
                           "New York", 7891957, 7781984, 
                           7894862, 7071639, 7322564, 8008278);


Creating the same tuple object by using a helper method is more straightforward, as the following example shows.


var population = Tuple.Create("New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278);


The Create() helper methods directly support the creation of tuple objects that have from one to eight components (that is, singletons through octuples). Although there is no practical limit to the number of components a tuple may have, helper methods are not available to create a tuple with nine or more components. To create such a tuple, you must call the Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>.Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> constructor.

Note Note

For additional information and examples that use tuples, see the documentation for the individual tuple types in the .NET Framework. These are listed in the See Also section at the end of this topic.

The following example creates an 8-tuple (octuple) that contains prime numbers that are less than 20.


var primes = Tuple.Create(2, 3, 5, 7, 11, 13, 17, 19);


.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Date

History

Reason

June 2011

Expanded the Remarks section.

Customer feedback.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
How do you get the data out?

There is no method documented here other than a constructor and a static 'create'.  The example is absolutely hilarious - just a constructor. 

So, um, how do you get data out?

The answer is you can access the elements as Item1, Item2, etc.  But, seems like an incredibly basic fact to leave out of the documentation, which makes this look like a write-once, non-readable datastructure.

Tuple Class and Tuples

I can see your point, but at the same time I don't agree. If other developers have strong feelings here, we'd like to hear about them. This topic has already been revised once to add content that we didn't feel belongs here, and we'd be happy to revise it again if there is sufficiently strong feeling.

In any case, the basic problem from a documentation perspective is that the Tuple class does not itself represent a Tuple. It's simply a factory class that allows you to create eight of the potentially infinite number of tuple types supported by the .NET Framework. The actual tuple types are represented by Tuple(<T1>) through Tuple(<T1>, T2, T3, T4, T5, T6, T7, TRest). As a factory class, Tuple is simply intended to provide convenience methods -- the same functionality is available from the individual tuple type's class constructor, but it requires a slightly greater degree of complexity and a good deal of additional coding. So the Tuple class can be used to create a tuple, but it can't be used to get data out, since it has only static factory methods. As a result, the "example" here is a one-liner that shows how to call a factory method and how that compares to a call to the corresponding class constructor; anything else simply would be out of place here. The documentation on getting data out, along with complete examples, is found in the documentation for each of the actual tuple types, which is where we believe that it logically belongs. We expect that developers who want information on the actual tuple types and their functionality to follow the appropriate link to a specific tuple type.

In any case, that accounts for why the documentation for the Tuple class appears as it does; it's a deliberate content decision. It also may be a poor decision. If so, we'd like to hear some additional opinions.

--Ron Petrusha
Common Language Runtime User Education
Microsoft Corporation

Tuple v Array?

If you want to pass data around and have a getter setter; why not use an array? What is the difference?

Why Not Use an Array?

Certainly an array is a viable option if you want to pass data to a method and be able to modify it. So is a custom data type, such as a program-defined structure or class. Which a developer chooses is often a matter of personal preference and programming style.

Tuples tend to have three major advantages:

  1. Unlike arrays, they are structured data types. Although you can impose a structure on arrays (for example, the first element of an array can represent an index, the second can represent a name, the third can represent an age, etc.), an array is not inherently structured. In contrast, a tuple type, as a generic type, is strongly typed at runtime. (Note that you could use a generic array type, such as List<T>, instead, but this requires that each element of the array have the same type. A tuple does not.)
  2. Because they are strongly typed, tuples should offer better performance than arrays. The performance improvement, however, is not likely to be signfiicant unless an application makes use of a large number of tuples.
  3. When you have to manipulate multiple items at the same time, using an array (or an ArrayList) of tuples is fairly intuitive, as long as you remember what each tuple item represents. Many of the tuple examples in our documentation, for instance, pass an List<T> of some tuple type to some routine for processing, sorting, or filtering. In contrast, if you choose to use an array, you have to use nested arrays. These are somewhat more cumbersome to work with, and some developers find them awkward, confusing, or difficult.

 --Ron Petrusha
Common Language Runtime User Education
Microsoft Corporation
 

Feature suggestion
It a step on the right direction, but still found wanted.
I think it would be extremely useful if you could add structural inheritance semantics into the tuples.
Together with named parameters, is could be so cool! e.g.,
//tuple that represents a person:
Tuple<int, String> preson = Tuple.Create(name: "John", age: 31);
//a tuple which represents an employee, which structurally inherits person:
Tumple<int, String, String> superHacker = Tuple.Create(name: "Elior", age:29, profession: "Programmer");
//and so (with some compiler modification), the following could be supported:
person = superHacker;

Extension of Tuple?

I've passed on your suggested modifications to the tuple types to the feature team. They will be considered for possible inclusion in a forthcoming version of the .NET Framework. Thanks for your feature suggestion.

--Ron Petrusha
Common Language Runtime User Education
Microsoft Corporation

So Tuples Prevent High Blood Pressure
@Matt: Then the Tuple is highly recommended by the American Heart Association.
Tuple items are READ ONLY
The documentation does not state this, but after creating the tuple, the items that are created by it are read-only as there is no setter.
What is a Tuple for?
Imagine that you want to work with some blood pressure data in a quick and dirty bit of code.  Instead of making a blood pressure class to just store systolic and diastolic values, maybe you just make a new Tuple<int, int> by calling Tuple.Create(120, 80).  A Tuple just lets you hold some data together in lieu of writing a whole class or struct to do it.  A two item tuple is a pair, a three item tuple is a triple, etc.  Anonymous types are typically localized to a method, so they don't work well for passing around.  Using a tuple lets you widen the scope and return tuples from methods.  My personal opinion is that they are useful for internal storage within a class where you might create an internal struct or class, but they shouldn't be exposed outside of class scope.