共用方式為


Tuple 類別

定義

提供靜態方法來建立元組物件。

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

範例

以下範例建立一個包含小於20的質數的8元組(八元組)。

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

備註

元組是一種具有特定元素數量與序列的資料結構。 元組的一個例子是包含三個元素的資料結構(稱為三元組或三元組),用來儲存識別碼,例如第一個元素中的個人姓名、第二個元素的年份,以及第三個元素中該人的當年度收入。 .NET Framework 直接支援元素為一到七個的元組。 此外,你還可以在物件屬性中巢狀元組RestTuple<T1,T2,T3,T4,T5,T6,T7,TRest>物件,建立八個或以上元素的元組。

元組通常有四種使用方式:

  • 用來表示一組資料。 例如,元組可以代表資料庫記錄,其組成部分可以代表記錄中的各個欄位。

  • 提供輕鬆存取與操作資料集的便利性。

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

  • 透過單一參數將多個值傳遞給一個方法。 例如,這個 Thread.Start(Object) 方法有一個參數,讓你在執行緒啟動時為執行程序提供一個值。 如果你提供 Tuple<T1,T2,T3> 一個物件作為方法參數,你就可以為執行緒的啟動例程提供三個資料項目。

這個 Tuple 類別本身並不代表一個元組。 它是一個類別,提供靜態方法來建立 .NET Framework 支援的元組類型實例。 它提供輔助方法,你可以呼叫來實例化元組物件,而不必明確指定每個元組元件的型別。

雖然你可以透過呼叫元組類別的建構子來建立一個實例,但執行此操作的程式碼可能會顯得繁瑣。 以下範例使用類別建構器建立一個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

使用輔助方法建立相同的元組物件較為直接,如下範例所示。

// 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<T1,T2,T3,T4,T5,T6,T7,TRest>.Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 構子。

備註

如需更多資訊及使用元組的範例,請參閱 .NET Framework 中各元組類型的文件。 這些分類列於本主題末尾的參見部分。

方法

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

建立一個新的 8 元組或八元組。

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

創造一個新的七元組,或稱七元組。

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

建立一個新的六元組,或稱六元組。

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

創造一個新的五元組或五元組。

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

創造一個新的四元組或四元組。

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

建立一個新的三元組,或三元組。

Create<T1,T2>(T1, T2)

建立一個新的 2 元組,或稱對。

Create<T1>(T1)

建立一個新的 1-元組,或稱單元。

適用於

另請參閱