Skip to main content
.NET Framework Class Library
KeyValuePairTKey, TValue Class

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

Inheritance Hierarchy
SystemObject
   SystemValueType
    System.Collections.GenericKeyValuePairTKey, TValue

Namespace:   System.Collections.Generic
Assemblies:   System.Runtime (in System.Runtime.dll)
  mscorlib (in mscorlib.dll)
Syntax
Public NotInheritable Class KeyValuePair(Of TKey, TValue) _
	Inherits [%$TOPIC/5tbh8a42_en-us_VS_110_2_0_0_0_0%]
public sealed class KeyValuePair<TKey, TValue> : [%$TOPIC/5tbh8a42_en-us_VS_110_2_0_1_0_0%]
generic<typename TKey, typename TValue>
public ref class KeyValuePair sealed : public [%$TOPIC/5tbh8a42_en-us_VS_110_2_0_2_0_0%]
[<[%$TOPIC/5tbh8a42_en-us_VS_110_2_0_3_0_0%]>]
type KeyValuePair<'TKey, 'TValue> =  
    class 
        inherit [%$TOPIC/5tbh8a42_en-us_VS_110_2_0_3_0_1%] 
    end

Type Parameters

TKey

The type of the key.

TValue

The type of the value.

The KeyValuePairTKey, TValue type exposes the following members.

Constructors
  NameDescription
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps KeyValuePairTKey, TValueInitializes a new instance of the KeyValuePairTKey, TValue structure with the specified key and value.
Top
Properties
  NameDescription
Public property Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps KeyGets the key in the key/value pair.
Public property Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps ValueGets the value in the key/value pair.
Top
Methods
  NameDescription
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps EqualsIndicates whether this instance and a specified object are equal. (Inherited from ValueType.)
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps GetHashCodeReturns the hash code for this instance. (Inherited from ValueType.)
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps GetTypeGets the Type of the current instance. (Inherited from Object.)
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps ToStringReturns a string representation of the KeyValuePairTKey, TValue, using the string representations of the key and value. (Overrides ValueTypeToString.)
Top
Remarks

The DictionaryTKey, TValueEnumeratorCurrent property returns an instance of this type.

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. Since each element of a collection based on IDictionaryTKey, TValue 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 KeyValuePairTKey, TValue. For example:

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

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

Examples

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

This code is part of a larger example provided for the DictionaryTKey, TValue 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
// 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( KeyValuePair<String^, String^> kvp in openWith )
{
    Console::WriteLine("Key = {0}, Value = {1}",
        kvp.Key, kvp.Value);
}
Version Information

.NET Framework

Supported in: 4.5.3, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Platforms

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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