KeyValuePair<TKey,TValue> Struktur

Definisi

Menentukan pasangan kunci/nilai yang dapat diatur atau diambil.

generic <typename TKey, typename TValue>
public value class KeyValuePair
public struct KeyValuePair<TKey,TValue>
public readonly struct KeyValuePair<TKey,TValue>
[System.Serializable]
public struct KeyValuePair<TKey,TValue>
type KeyValuePair<'Key, 'Value> = struct
[<System.Serializable>]
type KeyValuePair<'Key, 'Value> = struct
Public Structure KeyValuePair(Of TKey, TValue)

Jenis parameter

TKey

Jenis kunci.

TValue

Jenis nilai.

Warisan
KeyValuePair<TKey,TValue>
Atribut

Contoh

Contoh kode berikut menunjukkan cara menghitung kunci dan nilai dalam kamus, menggunakan KeyValuePair<TKey,TValue> struktur .

Kode ini adalah bagian dari contoh yang lebih besar yang disediakan untuk Dictionary<TKey,TValue> kelas .

// When you use foreach to enumerate dictionary elements,
// the elements are retrieved as KeyValuePair objects.
Console::WriteLine();
for each( KeyValuePair<String^, String^> kvp in openWith )
{
    Console::WriteLine("Key = {0}, Value = {1}",
        kvp.Key, kvp.Value);
}
// When you use foreach to enumerate dictionary elements,
// the elements are retrieved as KeyValuePair objects.
Console.WriteLine();
foreach( KeyValuePair<string, string> kvp in openWith )
{
    Console.WriteLine("Key = {0}, Value = {1}",
        kvp.Key, kvp.Value);
}
' When you use foreach to enumerate dictionary elements,
' the elements are retrieved as KeyValuePair objects.
Console.WriteLine()
For Each kvp As KeyValuePair(Of String, String) In openWith
    Console.WriteLine("Key = {0}, Value = {1}", _
        kvp.Key, kvp.Value)
Next kvp

Keterangan

Properti Dictionary<TKey,TValue>.Enumerator.Current mengembalikan instans jenis ini.

Pernyataan foreach bahasa C# (for each dalam C++, For Each di Visual Basic) mengembalikan objek dari jenis elemen dalam koleksi. Karena setiap elemen koleksi berdasarkan IDictionary<TKey,TValue> adalah pasangan kunci/nilai, jenis elemen bukan jenis kunci atau jenis nilai. Sebaliknya, jenis elemennya adalah KeyValuePair<TKey,TValue>. Contohnya:

for each(KeyValuePair<String^, String^> kvp in myDictionary)
{
    Console::WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}
foreach( KeyValuePair<string, string> kvp in myDictionary )
{
    Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}
For Each kvp As KeyValuePair(Of String, String) In myDictionary
    Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value)
Next kvp

Pernyataan tersebut foreach adalah pembungkus di sekitar enumerator, yang hanya memungkinkan membaca dari, tidak menulis ke, koleksi.

Konstruktor

KeyValuePair<TKey,TValue>(TKey, TValue)

Menginisialisasi instans KeyValuePair<TKey,TValue> baru struktur dengan kunci dan nilai yang ditentukan.

Properti

Key

Mendapatkan kunci dalam pasangan kunci/nilai.

Value

Mendapatkan nilai dalam pasangan kunci/nilai.

Metode

Deconstruct(TKey, TValue)

Mendekonstruksi saat ini KeyValuePair<TKey,TValue>.

ToString()

Mengembalikan representasi string dari KeyValuePair<TKey,TValue>, menggunakan representasi string kunci dan nilai.

Berlaku untuk

Lihat juga