Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 類別的新執行個體。
public:
Tuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7, TRest rest);
public Tuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7, TRest rest);
new Tuple<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7, 'Rest> : 'T1 * 'T2 * 'T3 * 'T4 * 'T5 * 'T6 * 'T7 * 'Rest -> Tuple<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7, 'Rest>
Public Sub New (item1 As T1, item2 As T2, item3 As T3, item4 As T4, item5 As T5, item6 As T6, item7 As T7, rest As TRest)
參數
- item1
- T1
元組的第一個組件值。
- item2
- T2
元組第二成分的值。
- item3
- T3
該元組第三個成分的值。
- item4
- T4
元組第四個成分的值。
- item5
- T5
元組第五分量的值。
- item6
- T6
元組第六個組件的值。
- item7
- T7
元組第七分量的值。
- rest
- TRest
任何包含元組剩餘組件值的通用 Tuple 物件。
例外狀況
rest 並非一個通用 Tuple 的物件。
範例
以下範例建立一個17元組,包含密西根州底特律市從1860年到2000年每次人口普查的人口資料。 元組的第一個組成部分是城市名稱。 第二個組成部分是資料序列的起始日期,第三個組成部分則是起始日期的人口。 每個後續組成部分每十年提供人口。 範例使用兩層巢狀結構來建立 17 元組:定義一個 7 元組,其第三至第七個組件包含 1860 年至 1900 年的族群資料;一個巢狀的 7 元組,包含 1910 年至 1970 年的族群資料;以及一個內嵌套的 3-元組,包含 1980 年至 2000 年的族群資料。
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)
備註
你也可以用靜態 Tuple.Create 方法實例化一個 8 元組(八元組)物件,而不必明確指定其組件的類型。 以下範例使用此 Tuple.Create 方法實例化一個包含小於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
這等同於以下對 Tuple<T1,T2,T3,T4,T5,T6,T7> 類別建構器的呼叫。
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))
然而,靜態 Tuple.Create 方法無法用來建立包含超過八個元件的元組物件。
當使用 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 建構子建立包含八個或以上組件的 n 元組時,你會使用參數 rest 來建立一個巢狀的 n 元組,該組包含從一到七個組件不等。 透過使用層級的巢狀設計,你可以建立一個包含幾乎無限數量元件的 n 元組。 例如,要建立一個 25 元組,你可以實例化一個 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 具有三層巢狀的物件,如下:
最 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> 狀物件包含第八至第十四個元件,其 Rest 性質可存取 Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 第二層巢狀物。
Tuple<T1,T2,T3,T4,T5,T6,T7,TRest>巢狀結構第二層的物件包含第十五至二十一步的元件,其Rest性質可存取Tuple<T1,T2,T3,T4>第三層巢式物件。
最內層的元組是一個 Tuple<T1,T2,T3,T4> 包含第22至25個組件的物件。