Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 類別

定義

代表 n-Tuple,其中 n 大於或等於 8。

generic <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename TRest>
public ref class Tuple : IComparable, System::Collections::IStructuralComparable, System::Collections::IStructuralEquatable
generic <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename TRest>
public ref class Tuple : IComparable, System::Collections::IStructuralComparable, System::Collections::IStructuralEquatable, System::Runtime::CompilerServices::ITuple
public class Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> : IComparable, System.Collections.IStructuralComparable, System.Collections.IStructuralEquatable
public class Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> : IComparable, System.Collections.IStructuralComparable, System.Collections.IStructuralEquatable, System.Runtime.CompilerServices.ITuple
[System.Serializable]
public class Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> : IComparable, System.Collections.IStructuralComparable, System.Collections.IStructuralEquatable
type Tuple<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7, 'Rest> = class
    interface IStructuralComparable
    interface IStructuralEquatable
    interface IComparable
type Tuple<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7, 'Rest> = class
    interface IStructuralComparable
    interface IStructuralEquatable
    interface IComparable
    interface ITuple
[<System.Serializable>]
type Tuple<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7, 'Rest> = class
    interface IStructuralEquatable
    interface IStructuralComparable
    interface IComparable
[<System.Serializable>]
type Tuple<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7, 'Rest> = class
    interface IStructuralEquatable
    interface IStructuralComparable
    interface IComparable
    interface ITuple
Public Class Tuple(Of T1, T2, T3, T4, T5, T6, T7, TRest)
Implements IComparable, IStructuralComparable, IStructuralEquatable
Public Class Tuple(Of T1, T2, T3, T4, T5, T6, T7, TRest)
Implements IComparable, IStructuralComparable, IStructuralEquatable, ITuple

類型參數

T1

Tuple 第 1 個元件的型別。

T2

Tuple 第 2 個元件的型別。

T3

Tuple 第 3 個元件的型別。

T4

Tuple 第 4 個元件的型別。

T5

Tuple 第 5 個元件的型別。

T6

Tuple 第 6 個元件的型別。

T7

Tuple 第 7 個元件的型別。

TRest

定義 Tuple 其餘元件的類型的任何泛型 Tuple 物件。

繼承
Tuple<T1,T2,T3,T4,T5,T6,T7,TRest>
屬性
實作

備註

Tuple 是具有特定數目和值序列的資料結構。 類別 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 代表具有八個以上元件的 n 元組。

您可以藉由呼叫靜態 Tuple.Create 方法,以具現化具有確切八個 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 元件的 物件。 下列範例會建立 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<T1,T2,T3,T4,T5,T6,T7,TRest> 建構函式來具現化具有八個或多個元件的 n-tuple 物件。 下列範例會使用 建 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 構函式來建立 8 元組,其相當於上一個範例中建立的 Tuple。

var primes = new Tuple<Int32, Int32, Int32, Int32, Int32, Int32, Int32,  
             Tuple<Int32>> (2, 3, 5, 7, 11, 13, 17, new Tuple<Int32>(19));
let primes = new Tuple<Int32, Int32, Int32, Int32, Int32, Int32, Int32,  
               Tuple<Int32>> (2, 3, 5, 7, 11, 13, 17, new Tuple<Int32>(19))
Dim primes = New Tuple(Of Int32, Int32, Int32, Int32, Int32, Int32, Int32, _ 
             Tuple(Of Int32))(2, 3, 5, 7, 11, 13, 17, New Tuple(Of Int32)(19))

注意

若要建立具有九個或多個元件的 n 元組,您必須呼叫 建 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 構函式。 類別的 Tuple 靜態處理站方法不支援建立 Tuple 具有八個以上的元件的物件。

若要具現化具有八個或多個元件的 n 元組與 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 建構函式,您必須提供泛型 Tuple 物件做為 rest 參數,以定義 Tuple 的第八個到 第 n 個元件。 透過以此方式巢狀泛型 Tuple 物件,您可以建立對其元件數目沒有任何實際限制的 Tuple。

下列範例會針對 1860 到 2000 年的每個國家人口普查,建立一個 17 元組,其中包含 D縣市的人口資料。 Tuple 的第一個元件是城市名稱。 第二個元件是資料數列的開始日期,而第三個元件則是開始日期的母體擴展。 每個後續元件會以十年間隔提供母體擴展。 17 元組是藉由將 物件內 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 的物件巢 Tuple<T1,T2,T3> 狀建立。 (也就是說, Tuple<T1,T2,T3> 物件會以類別建構函式中的 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 參數值 rest 的形式提供。) 這個 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 物件接著會巢狀于外部 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 物件中。 (也就是說, Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 物件會以外部 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 物件的類別建構函式中的 參數值 rest 的形式提供。)

var from1980 = Tuple.Create(1203339, 1027974, 951270);
var from1910 = new Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>> 
    (465766, 993078, 1568622, 1623452, 1849568, 1670144, 1511462, from1980);
var population = new Tuple<string, int, int, int, int, int, int,
    Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>>> 
    ("Detroit", 1860, 45619, 79577, 116340, 205876, 285704, from1910);
let from1980 = Tuple.Create(1203339, 1027974, 951270)
let from1910 = new Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>>(465766, 993078, 1568622, 1623452, 1849568, 1670144, 1511462, from1980)
let population = new Tuple<string, int, int, int, int, int, int, Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>>>("Detroit", 1860, 45619, 79577, 116340, 205876, 285704, from1910)
Dim from1980 = Tuple.Create(1203339, 1027974, 951270)
Dim from1910 As New Tuple(Of Integer, Integer, Integer, Integer, Integer, Integer, Integer, _
    Tuple(Of Integer, Integer, Integer)) _
    (465766, 993078, 1568622, 1623452, 1849568, 1670144, 1511462, from1980)
Dim population As New Tuple(Of String, Integer, Integer, Integer, Integer, Integer, Integer, _ 
    Tuple(Of Integer, Integer, Integer, Integer, Integer, Integer, Integer, Tuple(Of Integer, Integer, Integer))) _
    ("Detroit", 1860, 45619, 79577, 116340, 205876, 285704, from1910)

您可以使用唯讀 Item1Item2 、、 Item3Item4 Item5Item6Item7 實例屬性,擷取 Tuple 前七個元件的值。 任何其他元件都是巢狀元件,可以從 屬性擷 Rest 取。 在上述範例中,透過 Item7 屬性會 Item1 擷取 Tuple 的第一個到第七個元件。 第八到第十四個元件包含在第二個層級巢狀的 Tuple 中,並由 透過 Rest.Item7 屬性工作表示 Rest.Item1 。 第十五到十七個元件包含在第三個層級巢狀的 Tuple 中,並由 屬性 Rest.Rest.Item3 表示 Rest.Rest.Item1

Tuple 通常以四種不同的方式使用:

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

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

  • 若要從方法傳回多個值,而不在 C#) 中使用 out 參數 (,或在 ByRef Visual Basic) 中傳回參數 (。 例如,上一個範例會在 物件中 Tuple<T1,T2,T3,T4,T5,T6,T7> 傳回其計算統計資料以及城市名稱。

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

建構函式

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

初始化 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 類別的新執行個體。

屬性

Item1

取得目前 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 物件之第一個元件的值。

Item2

取得目前 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 物件之第二個元件的值。

Item3

取得目前 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 物件之第三個元件的值。

Item4

取得目前 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 物件之第四個元件的值。

Item5

取得目前 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 物件之第五個元件的值。

Item6

取得目前 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 物件之第六個元件的值。

Item7

取得目前 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 物件之第七個元件的值。

Rest

取得目前 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 物件的其餘元件。

方法

Equals(Object)

傳回值,這個值表示目前的 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 物件是否等於指定的物件。

GetHashCode()

計算目前 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 物件的雜湊碼。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回字串,表示這個 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 執行個體的值。

明確介面實作

IComparable.CompareTo(Object)

將目前的 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 物件與指定的物件比較,並傳回可指出目前物件在排序次序中,是否在指定物件之前、之後或者相同之位置的整數。

IStructuralComparable.CompareTo(Object, IComparer)

使用指定的比較子將目前的 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 物件和指定的物件進行比較,並且傳回一個整數,表示目前的物件在排序順序中位於指定之物件的前面、後面還是相的位置。

IStructuralEquatable.Equals(Object, IEqualityComparer)

傳回值,這個值表示依據指定的比較方法,目前的 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 物件是否等於指定的物件。

IStructuralEquatable.GetHashCode(IEqualityComparer)

使用指定的計算方法,計算目前 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 物件的雜湊碼。

ITuple.Item[Int32]

取得指定的 Tuple 項目值。

ITuple.Length

取得 Tuple 中的項目數目。

適用於

另請參閱