Dela via


IMap<K,V> Interface

Definition

Represents an associative collection, also known as a map or a dictionary.

.NET This interface appears to .NET code as System.Collections.Generic.IDictionary<TKey,TValue> due to .NET language projection. In any case where a Windows Runtime type has implemented IMap<K,V>, .NET code can use the APIs of IDictionary<TKey,TValue> instead.

public interface class IMap : IIterable<IKeyValuePair<K, V> ^>
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.FoundationContract, 65536)]
/// [Windows.Foundation.Metadata.Guid(1009329662, 34073, 17857, 170, 121, 25, 123, 103, 24, 193, 193)]
template <typename K, typename V>
struct IMap : IIterable<IKeyValuePair<K, V>>
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.FoundationContract), 65536)]
[Windows.Foundation.Metadata.Guid(1009329662, 34073, 17857, 170, 121, 25, 123, 103, 24, 193, 193)]
public interface IDictionary<K,V> : IEnumerable<KeyValuePair<K,V>>
Public Interface IDictionary(Of K, V)
Implements IEnumerable(Of KeyValuePair(Of K, V))

Type Parameters

K
V
Attributes
Implements

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.IDictionary<TKey,TValue> interface if they want to implement a map/dictionary type. In any case where a Windows Runtime type has implemented IMap<K,V>, .NET code can use the APIs of IDictionary<TKey,TValue> instead. This includes all the existing Windows Runtime API and also scenarios such as using the APIs of Windows Runtime components originally implemented in Visual C++ component extensions (C++/CX) from a C# or Visual Basic app.

The IMap<K,V> interface represents a collection of key-value pairs where a value can be accessed by its associated key. Properties and methods of IMap<K,V> support dictionary-type functionality, such as getting the size of the collection, and adding and removing items at specified locations in the collection. Additionally, the GetView method provides a snapshot of the map whose observable state does not change. The snapshot is useful when you need a view of the collection to refer to in subsequent operations that involve IMap<K,V>.

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.

auto begin() const;

Returns an iterator to the first key-value pair of the collection, for use in C++ algorithms such as range-based for loops.

auto end() const;

Returns an iterator to one past the last key-value pair of the collection, for use in C++ algorithms such as range-based for loops.

auto TryLookup(param_type<K> const& key) const;

Tries to look up an element in the map with the key key. For reference types, returns the value if found, or nullptr if not found. For value types, returns a std::optional<V>, which holds the value if found, or has no value if not found.

bool TryRemove(param_type<K> const& key) const;

Removes the element in the map with the key key, if present. Returns true if the element was found and removed. Returns false if the element was not found.

Interface inheritance

IMap<K,V> inherits IIterable<T>, using an IKeyValuePair constraint that uses the same key and value types. Types that implement IMap<K,V> also implement the interface members of IIterable<T>, with IKeyValuePair type constraint sharing the same key and value. Similarly, if you're using .NET, there is support for IEnumerabe<T>, with its constraint type as a KeyValuePair that uses the same key and value types as does the IDictionary<TKey,TValue> implementation.

Properties

Size

Gets the number of items in the map.

Methods

Clear()

Removes all items from the map.

GetView()

Returns an immutable view of the map.

HasKey(K)

Determines whether the map contains the specified key.

Insert(K, V)

Inserts or replaces an item in the map.

Lookup(K)

Returns the item at the specified key in the map.

Remove(K)

Removes an item from the map.

Applies to

See also