Export (0) Print
Expand All

KeyValuePair Generic Structure

Defines a key/value pair that can be set or retrieved.

Namespace: System.Collections.Generic
Assembly: mscorlib (in mscorlib.dll)

'Declaration
<SerializableAttribute> _
Public Structure KeyValuePair(Of TKey, TValue)
'Usage
Dim instance As KeyValuePair(Of TKey, TValue)

J# supports the use of generic types and methods, but not the declaration of new ones.
JScript does not support generic types and methods.
Not applicable.

Type Parameters

TKey

The type of the key.

TValue

The type of the value.

The Dictionary.Enumerator.Current property returns an instance of this type.

The foreach statement of the C# language (for each in C++, For Each in Visual Basic) requires the type of the elements in the collection. Since each element of a collection based on IDictionary is a key/value pair, the element type is not the type of the key or the type of the value. Instead, the element type is KeyValuePair. For example:

For Each kvp As KeyValuePair(Of Integer, String) In myDictionary
    ...
Next kvp

The foreach statement is a wrapper around the enumerator, which allows only reading from, not writing to, the collection.

The following code example shows how to enumerate the keys and values in a dictionary, using the KeyValuePair structure.

This code is part of a larger example provided for the Dictionary class.

' 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

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft