[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]
A tuple is a data structure that has a specific number and sequence of values. The Tuple<(Of <(T1, T2, T3, T4, T5, T6, T7, TRest>)>) class represents an n-tuple that has eight or more components.
You can instantiate a Tuple<(Of <(T1, T2, T3, T4, T5, T6, T7, TRest>)>) object with exactly eight components by calling the static Tuple..::.Create<(Of <(T1, T2, T3, T4, T5, T6, T7, T8>)>) method. The following example creates an 8-tuple (octuple) that contains prime numbers that are less than 20. Note that it uses type inference to determine the type of each component.
Dim primes = Tuple.Create(2, 3, 5, 7, 11, 13, 17, 19)
var primes = Tuple.Create(2, 3, 5, 7, 11, 13, 17, 19);
You can also instantiate an n-tuple object with eight or more components by calling the Tuple<(Of <(T1, T2, T3, T4, T5, T6, T7, TRest>)>) constructor. The following example uses the Tuple<(Of <(T1, T2, T3, T4, T5, T6, T7, TRest>)>) constructor to create an 8-tuple that is equivalent to the tuple created in the previous example.
Dim primes = New Tuple(Of Int32, Int32, Int32, Int32, Int32, Int32, Int32, _
Tuple(Of Int32))(2, 3, 5, 7, 11, 13, 17, New Tuple(Of Int32)(19))
var primes = new Tuple<Int32, Int32, Int32, Int32, Int32, Int32, Int32,
Tuple<Int32>> (2, 3, 5, 7, 11, 13, 17, new Tuple<Int32>(19));
To instantiate an n-tuple that has eight or more components with the Tuple<(Of <(T1, T2, T3, T4, T5, T6, T7, TRest>)>) constructor, you supply a generic Tuple object as the rest parameter to define the eighth through nth components of the tuple. By nesting generic Tuple objects in this way, you can create a tuple that has no practical limitation on the number of its components.
The following example creates a 17-tuple that contains population data for the city of Detroit, Michigan, for each national census from 1860 to 2000. The first component of the tuple is the city name. The second component is the start date of the data series, and the third component is the population at the start date. Each subsequent component provides the population at decade intervals. The 17-tuple is created by nesting a Tuple<(Of <(T1, T2, T3>)>) object inside a Tuple<(Of <(T1, T2, T3, T4, T5, T6, T7, TRest>)>) object. (That is, the Tuple<(Of <(T1, T2, T3>)>) object is supplied as the value of the rest parameter in the Tuple<(Of <(T1, T2, T3, T4, T5, T6, T7, TRest>)>) class constructor.) This Tuple<(Of <(T1, T2, T3, T4, T5, T6, T7, TRest>)>) object is, in turn, nested in an outer Tuple<(Of <(T1, T2, T3, T4, T5, T6, T7, TRest>)>) object. (That is, the Tuple<(Of <(T1, T2, T3, T4, T5, T6, T7, TRest>)>) object is supplied as the value of the rest parameter in the outer Tuple<(Of <(T1, T2, T3, T4, T5, T6, T7, TRest>)>) object's class constructor.)
Dim from1980 = Tuple.Create(1203339, 1027974, 951270)
Dim from1910 As New Tuple(Of Integer, Integer, Integer, Integer, Integer, Integer, Integer, _
Tuple(Of Integer, Integer, Integer)) _
(465766, 993078, 1568622, 1623452, 1849568, 1670144, 1511462, from1980)
Dim population As New Tuple(Of String, Integer, Integer, Integer, Integer, Integer, Integer, _
Tuple(Of Integer, Integer, Integer, Integer, Integer, Integer, Integer, Tuple(Of Integer, Integer, Integer))) _
("Detroit", 1860, 45619, 79577, 116340, 205876, 285704, from1910)
var from1980 = Tuple.Create(1203339, 1027974, 951270);
var from1910 = new Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>>
(465766, 993078, 1568622, 1623452, 1849568, 1670144, 1511462, from1980);
var population = new Tuple<string, int, int, int, int, int, int,
Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>>>
("Detroit", 1860, 45619, 79577, 116340, 205876, 285704, from1910);
You can retrieve the value of the tuple's first seven components by using the read-only Item1, Item2, Item3, Item4, Item5, Item6, and Item7 instance properties. Any additional components are nested and can be retrieved from the Rest property. In the previous example, the Item1 through Item7 properties retrieve the first through seventh components of the tuple. The eighth through fourteenth components are contained in the tuple that is nested at the second level, and are represented by the Rest.Item1 through Rest.Item7 properties. The fifteenth through seventeenth components are contained in the tuple that is nested at the third level, and are represented by the Rest.Rest.Item1 though Rest.Rest.Item3 properties.
Tuples are commonly used in four different 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 the use of out parameters (in C#) or ByRef parameters (in Visual Basic). For example, the previous example returns its computed statistics, along with the city name, in a Tuple<(Of <(T1, T2, T3, T4, T5, T6, T7>)>) object.
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. If you supply a Tuple<(Of <(T1, T2, T3, T4, T5, T6, T7>)>) object as the method argument, you can supply the thread’s startup routine with seven items of data.