KeyValuePair<TKey,TValue> KeyValuePair<TKey,TValue> KeyValuePair<TKey,TValue> KeyValuePair<TKey,TValue> Struct

定義

定義可設定或擷取的索引鍵/值組。Defines a key/value pair that can be set or retrieved.

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

類型參數

TKey

索引鍵的類型。The type of the key.

TValue

值的類型。The type of the value.

繼承
KeyValuePair<TKey,TValue>KeyValuePair<TKey,TValue>KeyValuePair<TKey,TValue>KeyValuePair<TKey,TValue>
屬性

範例

下列程式碼範例示範如何列舉索引鍵和值在字典中,使用KeyValuePair<TKey,TValue>結構。The following code example shows how to enumerate the keys and values in a dictionary, using the KeyValuePair<TKey,TValue> structure.

此程式碼是針對提供之較大範例的一部分Dictionary<TKey,TValue>類別。This code is part of a larger example provided for the Dictionary<TKey,TValue> class.

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

備註

Dictionary<TKey,TValue>.Enumerator.Current屬性會傳回這個型別的執行個體。The Dictionary<TKey,TValue>.Enumerator.Current property returns an instance of this type.

foreach陳述式的C#語言 (for each在C++, For Each Visual Basic 中) 傳回集合中的項目類型的物件。The foreach statement of the C# language (for each in C++, For Each in Visual Basic) returns an object of the type of the elements in the collection. 根據集合的每個項目自IDictionary<TKey,TValue>是索引鍵/值組,項目型別不是索引鍵的類型或值的類型。Since each element of a collection based on IDictionary<TKey,TValue> is a key/value pair, the element type is not the type of the key or the type of the value. 相反地,項目型別是KeyValuePair<TKey,TValue>Instead, the element type is KeyValuePair<TKey,TValue>. 例如:For example:

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

foreach陳述式是一個包裝函式可讓只讀取,不寫入至集合的列舉值。The foreach statement is a wrapper around the enumerator, which allows only reading from, not writing to, the collection.

建構函式

KeyValuePair<TKey,TValue>(TKey, TValue) KeyValuePair<TKey,TValue>(TKey, TValue) KeyValuePair<TKey,TValue>(TKey, TValue) KeyValuePair<TKey,TValue>(TKey, TValue)

使用指定的索引鍵和值,初始化 KeyValuePair<TKey,TValue> 結構的新執行個體。Initializes a new instance of the KeyValuePair<TKey,TValue> structure with the specified key and value.

屬性

Key Key Key Key

取得索引鍵/值組中的索引鍵。Gets the key in the key/value pair.

Value Value Value Value

取得索引鍵/值組中的值。Gets the value in the key/value pair.

方法

Deconstruct(TKey, TValue) Deconstruct(TKey, TValue) Deconstruct(TKey, TValue) Deconstruct(TKey, TValue)
ToString() ToString() ToString() ToString()

使用索引鍵和值的字串表示,傳回 KeyValuePair<TKey,TValue> 的字串表示。Returns a string representation of the KeyValuePair<TKey,TValue>, using the string representations of the key and value.

適用於

另請參閱