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

Definicja

Inicjuje nowe wystąpienie klasy 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)

Parametry

item1
T1

Wartość pierwszego składnika spójnej kolekcji.

item2
T2

Wartość drugiego składnika spójnej kolekcji.

item3
T3

Wartość trzeciego składnika spójnej kolekcji.

item4
T4

Wartość czwartego składnika spójnej kolekcji.

item5
T5

Wartość piątego składnika spójnej kolekcji.

item6
T6

Wartość szóstego składnika krotki.

item7
T7

Wartość siódmego składnika krotki.

rest
TRest

Dowolny obiekt ogólny Tuple , który zawiera wartości pozostałych składników krotki.

Wyjątki

rest nie jest obiektem ogólnym Tuple .

Przykłady

Poniższy przykład obejmuje tworzenie 17 krotki zawierającej dane dotyczące populacji miasta Detroit w stanie Michigan dla każdego spisu z 1860 do 2000 roku. Pierwszym składnikiem krotki jest nazwa miasta. Drugi składnik to data rozpoczęcia serii danych, a trzeci składnik to populacja w dacie początkowej. Każdy kolejny składnik zapewnia populację w interwałach dekad. W przykładzie użyto dwóch warstw zagnieżdżania, aby utworzyć krotkę 17: definiuje 7-krotkę, której trzecie do siódmych składników zawiera dane populacji dla 1860 do 1900, zagnieżdżonych krotki 7, która zawiera dane populacji dla 1910 do 1970, oraz wewnętrzną zagnieżdżonych 3 krotki, która zawiera dane populacji dla 1980 do 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)

Uwagi

Można również użyć metody statycznej Tuple.Create , aby utworzyć wystąpienie obiektu krotki 8-krotki (ósemki) bez konieczności jawnego określania typów jego składników. W poniższym przykładzie użyto Tuple.Create metody do utworzenia wystąpienia obiektu krotki 8-krotki zawierającej liczby pierwsze, które są mniejsze niż 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

Jest to odpowiednik następującego wywołania konstruktora Tuple<T1,T2,T3,T4,T5,T6,T7> klasy.

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))

Nie można jednak użyć metody statycznej Tuple.Create do utworzenia obiektu krotki z ponad ośmioma składnikami.

Używając konstruktora Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> do utworzenia n-krotki z ośmioma lub większą większa większa liczba składników, należy użyć parametru rest do utworzenia zagnieżdżonej n-krotki zawierającej od jednego do siedmiu składników. Za pomocą kolejnych poziomów zagnieżdżania można utworzyć n-krotkę, która ma praktycznie nieograniczoną liczbę składników. Aby na przykład utworzyć krotkę Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> 25, należy utworzyć wystąpienie obiektu z trzema poziomami zagnieżdżania w następujący sposób:

Dotyczy

Zobacz też