Tuple 類別

定義

提供用於建立 Tuple 物件的靜態方法。

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

範例

下列範例會建立 8 元組 (八進位) ,其中包含小於 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
open System

let primes = Tuple.Create(2, 3, 5, 7, 11, 13, 17, 19)
printfn $"Prime numbers less than 20: {primes.Item1}, {primes.Item2}, {primes.Item3}, {primes.Item4}, {primes.Item5}, {primes.Item6}, {primes.Item7}, and {primes.Rest.Item1}"
//    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

備註

Tuple 是具有特定專案數目和序列的資料結構。 元組的範例是資料結構,其中三個元素 (稱為 3 元組或三個) ,用來儲存第一個元素中的識別碼、第二個元素中的年份,以及第三個元素中該年份的人員收入。 .NET Framework直接支援具有一到七個元素的元組。 此外,您可以在 物件的 屬性中 Rest 巢狀 Tuple 物件,以建立八個或多個元素的 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 元組。

Tuple 通常以四種方式使用:

  • 表示單一資料集。 例如,Tuple 可以代表資料庫記錄,而其元件可以代表記錄的個別欄位。

  • 若要提供資料集的輕鬆存取和操作。

  • 若要從方法傳回多個值,而不在 C#) 中使用 out 參數 (,或在 ByRef Visual Basic) 中使用參數 (。

  • 透過單一參數將多個值傳遞至方法。 例如, Thread.Start(Object) 方法具有單一參數,可讓您將一個值提供給執行緒在啟動時執行的方法。 如果您提供 Tuple<T1,T2,T3> 物件做為方法引數,則可以提供執行緒的啟動常式,其中包含三個專案的資料。

類別 Tuple 本身並不代表元組。 相反地,它是一個類別,提供靜態方法來建立.NET Framework所支援的 Tuple 型別實例。 它提供可呼叫具現化 Tuple 物件的協助程式方法,而不需要明確指定每個 Tuple 元件的類型。

雖然您可以藉由呼叫 Tuple 類別建構函式來建立 Tuple 類別的實例,但要這樣做的程式碼可能很麻煩。 下列範例使用類別建構函式來建立 7 元組或九月,其中包含 1950 到 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.
let population = Tuple<string, int, int, int, int, int, int>(
                            "New York", 7891957, 7781984, 
                            7894862, 7071639, 7322564, 8008278)
// Display the first and last elements.
printfn $"Population of {population.Item1} in 2000: {population.Item7:N0}"
// 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

使用協助程式方法建立相同的 Tuple 物件更為簡單,如下列範例所示。

// 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.
let population = Tuple.Create("New York", 7891957, 7781984, 7894862, 7071639, 7322564, 8008278)
// Display the first and last elements.
printfn $"Population of {population.Item1} in 2000: {population.Item7:N0}"
// 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 程式方法直接支援建立從一到八個元件 (的 Tuple 物件,也就是透過八位) 的單一元件。 雖然 Tuple 可能擁有的元件數目沒有實際限制,但協助程式方法無法建立具有九個或更多元件的 Tuple。 若要建立這類元組,您必須呼叫 建 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest>.Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 構函式。

注意

如需使用 Tuple 的其他資訊和範例,請參閱.NET Framework中個別 Tuple 類型的檔。 這些列于本主題結尾的一節。

方法

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

建立新的 8-tuple 或八重 (Octuple) 物件。

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

建立新的 7-Tuple 或七重 (Septuple) 物件。

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

建立新的 6-Tuple 或六重 (Sextuple) 物件。

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

建立新的 5-Tuple 或五重 (Quintuple) 物件。

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

建立新的 4-Tuple 或四重 (Quadruple) 物件。

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

建立新的 3-Tuple 或三重 (Triple) 物件。

Create<T1,T2>(T1, T2)

建立新的 2-Tuple 或雙重 (Pair) 物件。

Create<T1>(T1)

建立新的 1-Tuple 或單一 (Singleton) 物件。

適用於

另請參閱