Tuple Class
Updated: June 2011
Provides static methods for creating tuple objects.
Assembly: mscorlib (in mscorlib.dll)
| Name | Description | |
|---|---|---|
|
Create<T1>(T1) | Creates a new 1-tuple, or singleton. |
|
Create<T1, T2>(T1, T2) | Creates a new 2-tuple, or pair. |
|
Create<T1, T2, T3>(T1, T2, T3) | Creates a new 3-tuple, or triple. |
|
Create<T1, T2, T3, T4>(T1, T2, T3, T4) | Creates a new 4-tuple, or quadruple. |
|
Create<T1, T2, T3, T4, T5>(T1, T2, T3, T4, T5) | Creates a new 5-tuple, or quintuple. |
|
Create<T1, T2, T3, T4, T5, T6>(T1, T2, T3, T4, T5, T6) | Creates a new 6-tuple, or sextuple. |
|
Create<T1, T2, T3, T4, T5, T6, T7>(T1, T2, T3, T4, T5, T6, T7) | Creates a new 7-tuple, or septuple. |
|
Create<T1, T2, T3, T4, T5, T6, T7, T8>(T1, T2, T3, T4, T5, T6, T7, T8) | Creates a new 8-tuple, or octuple. |
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
|
|---|
|
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.
Creating the same tuple object by using a helper method is more straightforward, as the following example shows.
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
|
|---|
|
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. |
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.
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
- 2/15/2012
- Tom Lianza
- 2/17/2012
- R Petrusha - MSFT
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:
- 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.)
- 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.
- 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
- 1/17/2012
- philn5d
- 2/7/2012
- R Petrusha - MSFT
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
- 1/30/2012
- parkerAntony
- 2/7/2012
- R Petrusha - MSFT
- 9/1/2011
- thenonhacker
- 5/23/2011
- Jeremy Polen
- 5/17/2011
- MattMc3
Note