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

通过使用一个帮助器方法创建的同一个元组对象是更简单,如以下示例所示。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帮助器方法直接支持包含一至八个组件 (即,单例到八元组) 的元组对象的创建。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.

适用于

另请参阅