Tuple Tuple Tuple Tuple Class

定義

提供用於建立 Tuple 物件的靜態方法。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元組 (octuple), 其中包含小於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 直接支援具有一到七個元素的元組。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. 它提供 helper 方法, 可讓您呼叫來具現化元組物件, 而不需要明確指定每個元組元件的類型。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元組或 septuple, 其中包含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). 雖然元組可能有元件數目的實際限制, 但無法使用 helper 方法來建立包含九個或更多元件的元組。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-tuple 或八重 (Octuple) 物件。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-Tuple 或七重 (Septuple) 物件。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-Tuple 或六重 (Sextuple) 物件。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-Tuple 或五重 (Quintuple) 物件。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-Tuple 或四重 (Quadruple) 物件。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-Tuple 或三重 (Triple) 物件。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-Tuple 或雙重 (Pair) 物件。Creates a new 2-tuple, or pair.

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

建立新的 1-Tuple 或單一 (Singleton) 物件。Creates a new 1-tuple, or singleton.

適用於

另請參閱