Tuple Tuple Tuple Tuple Class

定义

提供用于创建元组对象的静态方法。Provides static methods for creating tuple objects.

public ref class Tuple abstract sealed
public static class Tuple
type Tuple = class
Public Class Tuple
继承
TupleTupleTupleTuple

示例

下面的示例创建一个8元组 (八元组), 其中包含小于20的质数。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.Item4, primes.Item5, primes.Item6,
                  primes.Item7, primes.Rest.Item1);
// The example displays the following output:
//    Prime numbers less than 20: 2, 3, 5, 7, 11, 13, 17, and 19
Dim 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.Item4, primes.Item5, primes.Item6,
                  primes.Item7, primes.Rest.Item1)
' The example displays the following output:
'     Prime numbers less than 20: 2, 3, 5, 7, 11, 13, 17, and 19

注解

元组是具有特定数量和元素序列的数据结构。A tuple is a data structure that has a specific number and sequence of elements. 元组的示例是一个包含三个元素 (称为3元组或三个元素) 的数据结构, 用于存储标识符, 如第一个元素中的人员姓名、第二个元素中的年份, 以及第三个元素中该年份的用户收入。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. .NET Framework 直接支持包含1到7个元素的元组。The .NET Framework directly supports tuples with one to seven elements. 此外, 通过在Rest Tuple<T1,T2,T3,T4,T5,T6,T7,TRest>对象的属性中嵌套元组对象, 可以创建八个或多个元素的元组。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.

  • 从方法返回多个值, 而不out使用参数 ( C#在中ByRef ) 或参数 (在 Visual Basic 中)。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. 例如, 该Thread.Start(Object)方法只有一个参数, 该参数可让你为线程在启动时执行的方法提供一个值。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. 如果提供Tuple<T1,T2,T3>对象作为方法参数, 则可以为该线程的启动例程提供三个数据项。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.

Tuple类本身不表示元组。The Tuple class does not itself represent a tuple. 相反, 它是一个类, 该类提供静态方法用于创建 .NET Framework 支持的元组类型的实例。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. 下面的示例使用类构造函数创建一个7元组或七元组, 其中包含从1950到2000的每个人口普查的纽约人口数据。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
' Create a 7-tuple.
Dim population As New Tuple(Of String, Integer, Integer, Integer, Integer, Integer, Integer) _
                           ("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

使用 helper 方法创建相同的元组对象更简单, 如下面的示例所示。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
' Create a 7-tuple.
Dim 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

Create Helper 方法直接支持创建包含一到八个组件 (即单一实例到 octuples) 的元组对象。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. 若要创建此类元组, 必须调用Tuple<T1,T2,T3,T4,T5,T6,T7,TRest>.Tuple<T1,T2,T3,T4,T5,T6,T7,TRest>构造函数。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.

备注

有关使用元组的其他信息和示例, 请参阅 .NET Framework 中各个元组类型的文档。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.

方法

Create<T1,T2,T3,T4,T5,T6,T7,T8>(T1, T2, T3, T4, T5, T6, T7, T8) Create<T1,T2,T3,T4,T5,T6,T7,T8>(T1, T2, T3, T4, T5, T6, T7, T8) Create<T1,T2,T3,T4,T5,T6,T7,T8>(T1, T2, T3, T4, T5, T6, T7, T8) Create<T1,T2,T3,T4,T5,T6,T7,T8>(T1, T2, T3, T4, T5, T6, T7, T8)

创建新的 8 元组,即八元组。Creates a new 8-tuple, or octuple.

Create<T1,T2,T3,T4,T5,T6,T7>(T1, T2, T3, T4, T5, T6, T7) Create<T1,T2,T3,T4,T5,T6,T7>(T1, T2, T3, T4, T5, T6, T7) Create<T1,T2,T3,T4,T5,T6,T7>(T1, T2, T3, T4, T5, T6, T7) Create<T1,T2,T3,T4,T5,T6,T7>(T1, T2, T3, T4, T5, T6, T7)

创建新的 7 元组,即七元组。Creates a new 7-tuple, or septuple.

Create<T1,T2,T3,T4,T5,T6>(T1, T2, T3, T4, T5, T6) Create<T1,T2,T3,T4,T5,T6>(T1, T2, T3, T4, T5, T6) Create<T1,T2,T3,T4,T5,T6>(T1, T2, T3, T4, T5, T6) Create<T1,T2,T3,T4,T5,T6>(T1, T2, T3, T4, T5, T6)

创建新的 6 元组,即六元组。Creates a new 6-tuple, or sextuple.

Create<T1,T2,T3,T4,T5>(T1, T2, T3, T4, T5) Create<T1,T2,T3,T4,T5>(T1, T2, T3, T4, T5) Create<T1,T2,T3,T4,T5>(T1, T2, T3, T4, T5) Create<T1,T2,T3,T4,T5>(T1, T2, T3, T4, T5)

创建新的 5 元组,即五元组。Creates a new 5-tuple, or quintuple.

Create<T1,T2,T3,T4>(T1, T2, T3, T4) Create<T1,T2,T3,T4>(T1, T2, T3, T4) Create<T1,T2,T3,T4>(T1, T2, T3, T4) Create<T1,T2,T3,T4>(T1, T2, T3, T4)

创建新的 4 元组,即四元组。Creates a new 4-tuple, or quadruple.

Create<T1,T2,T3>(T1, T2, T3) Create<T1,T2,T3>(T1, T2, T3) Create<T1,T2,T3>(T1, T2, T3) Create<T1,T2,T3>(T1, T2, T3)

创建新的 3 元组,即三元组。Creates a new 3-tuple, or triple.

Create<T1,T2>(T1, T2) Create<T1,T2>(T1, T2) Create<T1,T2>(T1, T2) Create<T1,T2>(T1, T2)

创建新的 2 元组,即二元组。Creates a new 2-tuple, or pair.

Create<T1>(T1) Create<T1>(T1) Create<T1>(T1) Create<T1>(T1)

创建新的 1 元组,即单一实例。Creates a new 1-tuple, or singleton.

适用于

另请参阅