Export (0) Print
Expand All

Tuple Class

Provides static methods for creating tuple objects.

To browse the .NET Framework source code for this type, see the Reference Source.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static class Tuple

  NameDescription
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsCreate<T1>(T1)Creates a new 1-tuple, or singleton.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsCreate<T1, T2>(T1, T2)Creates a new 2-tuple, or pair.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsCreate<T1, T2, T3>(T1, T2, T3)Creates a new 3-tuple, or triple.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsCreate<T1, T2, T3, T4>(T1, T2, T3, T4)Creates a new 4-tuple, or quadruple.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsCreate<T1, T2, T3, T4, T5>(T1, T2, T3, T4, T5)Creates a new 5-tuple, or quintuple.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsCreate<T1, T2, T3, T4, T5, T6>(T1, T2, T3, T4, T5, T6)Creates a new 6-tuple, or sextuple.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsCreate<T1, T2, T3, T4, T5, T6, T7>(T1, T2, T3, T4, T5, T6, T7)Creates a new 7-tuple, or septuple.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsCreate<T1, T2, T3, T4, T5, T6, T7, T8>(T1, T2, T3, T4, T5, T6, T7, T8)Creates a new 8-tuple, or octuple.
Top

NoteNote

To view the .NET Framework source code for this type, see the Reference Source. You can browse through the source code online, download the reference for offline viewing, and step through the sources (including patches and updates) during debugging; see instructions.

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

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.

// Create a 7-tuple. 
var population = new Tuple<string, int, int, int, int, int, int>(
                           "New York", 7891957, 7781984, 
                           7894862, 7071639, 7322564, 8008278);
// Display the first and last elements.
Console.WriteLine("Population of {0} in 2000: {1:N0}",
                  population.Item1, population.Item7);
// The example displays the following output: 
//       Population of New York in 2000: 8,008,278 
//       Population of New York in 2000: 8,008,278

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

// Create a 7-tuple. 
var population = Tuple.Create("New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278);
// Display the first and last elements.
Console.WriteLine("Population of {0} in 2000: {1:N0}",
                  population.Item1, population.Item7);
// The example displays the following output: 
//       Population of New York in 2000: 8,008,278 
//       Population of New York in 2000: 8,008,278

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.

NoteNote

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);
Console.WriteLine("Prime numbers less than 20: " + 
                  "{0}, {1}, {2}, {3}, {4}, {5}, {6}, and {7}",
                  primes.Item1, primes.Item2, primes.Item3, 
                  primes.Item5, primes.Item5, primes.Item6,
                  primes.Item7, primes.Rest.Item1);
// The example displays the following output: 
//    Prime numbers less than 20: 2, 3, 5, 11, 11, 13, 17, and 19

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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.
Show:
© 2014 Microsoft