IKeyValuePair<K,V> 介面

定義

表示索引鍵/值組。 當您需要將兩個型別參數封裝成一個型別參數,以滿足另一個泛型介面的條件約束時,這通常用來作為條件約束類型。

。網 此介面會顯示為 System.Collections.Generic.KeyValuePair<TKey,TValue> (結構,而不是介面) 。 在任何 Windows 執行階段 類型已實作 IKeyValuePair<K、V> 的情況下,.NET 程式代碼都可以改用 KeyValuePair 的 API。

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)

類型參數

K
V
屬性

Windows 需求

裝置系列
Windows 10 (已於 10.0.10240.0 引進)
API contract
Windows.Foundation.FoundationContract (已於 v1.0 引進)

備註

使用 .NET 進行程序設計時,此介面會隱藏,開發人員應該使用 System.Collections.Generic.KeyValuePair<TKey,TValue> 結構。 在 Windows 執行階段 類型已實作 IKeyValuePair<K、V> 的情況下,包括當介面繼承 IKeyValuePair<K、V> 或使用它作為內部條件約束時,.NET 程式代碼可以將它視為 .NET KeyValuePair

索引鍵/值組會在繼承 IIterable<T> 時用於 IMap<K、V> 介面中。 IMap<K、V> 的實際實作是可反覆運算的,而反覆執行或呼叫 First/Current 會使用與 IMap<K、V 實作相同的條件約束,傳回 IKeyValuePair<K、V>> 的實例。

C++/WinRT 擴充功能函式

注意

延伸模組函式存在於特定 Windows 執行階段 API 的 C++/WinRT 投影類型上。 例如,winrt::Windows::Foundation::IAsyncAction 是 IAsyncAction 的 C++/WinRT 投影類型。 擴充函式不是應用程式二進位介面的一部分, (實際 Windows 執行階段 類型的 ABI) 介面,因此它們不會列為 Windows 執行階段 API 的成員。 但您可以從任何 C++/WinRT 專案內呼叫它們。 請參閱擴充 Windows 執行階段 API 的 C++/WinRT 函式

operator==

比較兩個 KeyValuePair 對象,比較索引鍵和值,而不是比較介面。

結構化系結 (C++/WinRT)

IKeyValuePair<K、V> 支援結構化系結。 例如

auto&& [key, value] = kvp;

相當於,

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

結構化系結在範圍型 for 循環中特別方便,可讓您逐一查看地圖的索引鍵/值組。

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

屬性

Key

取得索引鍵/值組的索引鍵。

Value

取得索引鍵/值組的值。

適用於

另請參閱