Bagikan melalui


Tuple Kelas

Definisi

Menyediakan metode statis untuk membuat objek tuple.

public ref class Tuple abstract sealed
public static class Tuple
type Tuple = class
Public Class Tuple
Warisan
Tuple

Contoh

Contoh berikut membuat 8 tuple (octuple) yang berisi bilangan prima yang kurang dari 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

Keterangan

Tuple adalah struktur data yang memiliki jumlah dan urutan elemen tertentu. Contoh tuple adalah struktur data dengan tiga elemen (dikenal sebagai 3 tuple atau tiga kali lipat) yang digunakan untuk menyimpan pengidentifikasi seperti nama seseorang dalam elemen pertama, satu tahun di elemen kedua, dan pendapatan orang untuk tahun itu di elemen ketiga. .NET Framework secara langsung mendukung tuple dengan satu hingga tujuh elemen. Selain itu, Anda dapat membuat tuple dari delapan elemen atau lebih dengan menyarangkan objek tuple di Rest properti Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> objek.

Tuple umumnya digunakan dalam empat cara:

  • Untuk mewakili satu set data. Misalnya, tuple dapat mewakili rekaman database, dan komponennya dapat mewakili bidang rekaman individual.

  • Untuk menyediakan akses mudah ke, dan manipulasi, himpunan data.

  • Untuk mengembalikan beberapa nilai dari metode tanpa menggunakan out parameter (dalam C#) atau ByRef parameter (di Visual Basic).

  • Untuk meneruskan beberapa nilai ke metode melalui satu parameter. Misalnya, Thread.Start(Object) metode ini memiliki satu parameter yang memungkinkan Anda memberikan satu nilai ke metode yang dijalankan utas pada waktu startup. Jika Anda menyediakan Tuple<T1,T2,T3> objek sebagai argumen metode, Anda dapat menyediakan rutinitas startup utas dengan tiga item data.

Kelas Tuple itu sendiri tidak mewakili tuple. Sebaliknya, ini adalah kelas yang menyediakan metode statis untuk membuat instans jenis tuple yang didukung oleh .NET Framework. Ini menyediakan metode pembantu yang dapat Anda panggil untuk membuat instans objek tuple tanpa harus secara eksplisit menentukan jenis setiap komponen tuple.

Meskipun Anda dapat membuat instans kelas tuple dengan memanggil konstruktor kelasnya, kode untuk melakukannya bisa rumit. Contoh berikut menggunakan konstruktor kelas untuk membuat 7 tuple atau septuple yang berisi data populasi untuk Kota New York untuk setiap sensus dari 1950 hingga 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

Membuat objek tuple yang sama dengan menggunakan metode pembantu lebih mudah, seperti yang ditunjukkan contoh berikut.

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

Metode Create pembantu secara langsung mendukung pembuatan objek tuple yang memiliki dari satu hingga delapan komponen (yaitu, singleton melalui oktet). Meskipun tidak ada batas praktis untuk jumlah komponen yang mungkin dimiliki tuple, metode pembantu tidak tersedia untuk membuat tuple dengan sembilan komponen atau lebih. Untuk membuat tuple seperti itu Tuple<T1,T2,T3,T4,T5,T6,T7,TRest>.Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> , Anda harus memanggil konstruktor.

Nota

Untuk informasi dan contoh tambahan yang menggunakan tuple, lihat dokumentasi untuk jenis tuple individual di .NET Framework. Ini tercantum di bagian Lihat Juga di akhir topik ini.

Metode

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

Membuat 8 tuple baru, atau oktuple.

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

Membuat 7 tuple baru, atau septuple.

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

Membuat 6 tuple baru, atau sextuple.

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

Membuat 5 tuple baru, atau quintuple.

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

Membuat 4 tuple baru, atau quadruple.

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

Membuat 3 tuple baru, atau tiga kali lipat.

Create<T1,T2>(T1, T2)

Membuat 2 tuple baru, atau pasangan.

Create<T1>(T1)

Membuat 1 tuple baru, atau singleton.

Berlaku untuk

Lihat juga