Share via


IKeyValuePair<K,V> Interface

Definition

Represents a key-value pair. This is typically used as a constraint type when you need to encapsulate two type parameters into one to satisfy the constraints of another generic interface.

Note

In .NET, this interface appears as System.Collections.Generic.KeyValuePair<TKey,TValue> (a structure, not an interface). In any case where a Windows Runtime type has implemented IKeyValuePair<K,V>, .NET code can use the APIs of KeyValuePair instead.

public interface class IKeyValuePair
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.FoundationContract, 65536)]
/// [Windows.Foundation.Metadata.Guid(45422889, 49604, 19070, 137, 64, 3, 18, 181, 193, 133, 0)]
template <typename K, typename V>
struct IKeyValuePair
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.FoundationContract), 65536)]
[Windows.Foundation.Metadata.Guid(45422889, 49604, 19070, 137, 64, 3, 18, 181, 193, 133, 0)]
public interface KeyValuePair<K,V>
Public Interface KeyValuePair(Of K, V)

Type Parameters

K
V
Attributes

Windows requirements

Device family
Windows 10 (introduced in 10.0.10240.0)
API contract
Windows.Foundation.FoundationContract (introduced in v1.0)

Remarks

When programming with .NET, this interface is hidden and developers should use the System.Collections.Generic.KeyValuePair<TKey,TValue> structure. In any case where a Windows Runtime type has implemented IKeyValuePair<K,V>, including when interfaces have inherited IKeyValuePair<K,V> or used it as an inner constraint, .NET code can treat it as a .NET KeyValuePair.

Key-value pairs are used in the IMap<K,V> interface, when it inherits IIterable<T>. Practical implementations of IMap<K,V> are iterable, and iterating or calling First/Current explicitly will return instances of IKeyValuePair<K,V> using the same constraints as does the IMap<K,V> implementation.

C++/WinRT extension functions

Note

Extension functions exist on the C++/WinRT projection types for certain Windows Runtime APIs. For example, winrt::Windows::Foundation::IAsyncAction is the C++/WinRT projection type for IAsyncAction. The extension functions aren't part of the application binary interface (ABI) surface of the actual Windows Runtime types, so they're not listed as members of the Windows Runtime APIs. But you can call them from within any C++/WinRT project. See C++/WinRT functions that extend Windows Runtime APIs.

operator==

Comparing two KeyValuePair objects, compare the keys and values rather than comparing the interfaces.

Structured binding (C++/WinRT)

IKeyValuePair<K, V> supports structured binding. For example,

auto&& [key, value] = kvp;

is equivalent to,

auto key = kvp.Key();
auto value = kvp.Value();

Structured binding is particularly convenient in range-based for loops, letting you iterate through the key/value pairs of a map.

winrt::Windows::Foundation::Collections::IMap<K, V> map;
for (auto&& [key, value] : map) { ... }

Properties

Key

Gets the key of the key-value pair.

Value

Gets the value of the key-value pair.

Applies to

See also