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.

适用于

另请参阅