KeyValuePair<TKey,TValue> 結構

定義

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

generic <typename TKey, typename TValue>
public value class KeyValuePair
public 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)

類型參數

TKey

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

TValue

值的型別。The type of the value.

繼承
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.

C foreach + + 中的 c # 語言 (的語句 for eachFor 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> 結構的新執行個體。Initializes a new instance of the KeyValuePair<TKey,TValue> structure with the specified key and value.

屬性

Key

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

Value

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

方法

Deconstruct(TKey, TValue)

解構為目前的 KeyValuePair<TKey,TValue>Deconstructs the current KeyValuePair<TKey,TValue>.

ToString()

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

適用於

另請參閱