共用方式為


Dictionary<TKey,TValue>.Keys 屬性

定義

取得集合,包含 Dictionary<TKey,TValue> 中的索引鍵。

public:
 property System::Collections::Generic::Dictionary<TKey, TValue>::KeyCollection ^ Keys { System::Collections::Generic::Dictionary<TKey, TValue>::KeyCollection ^ get(); };
public System.Collections.Generic.Dictionary<TKey,TValue>.KeyCollection Keys { get; }
member this.Keys : System.Collections.Generic.Dictionary<'Key, 'Value>.KeyCollection
Public ReadOnly Property Keys As Dictionary(Of TKey, TValue).KeyCollection

屬性值

Dictionary<TKey,TValue>.KeyCollection,包含 Dictionary<TKey,TValue> 中的索引鍵。

範例

下列程式代碼範例示範如何使用 屬性列舉字典 Keys 中的索引鍵,以及如何列舉字典中的索引鍵和值。

此程式代碼是可編譯和執行之較大範例的一部分, openWith (是本範例中使用的字典名稱) 。 請參閱 Dictionary<TKey,TValue>

// To get the keys alone, use the Keys property.
Dictionary<String^, String^>::KeyCollection^ keyColl =
    openWith->Keys;

// The elements of the KeyCollection are strongly typed
// with the type that was specified for dictionary keys.
Console::WriteLine();
for each( String^ s in keyColl )
{
    Console::WriteLine("Key = {0}", s);
}
// To get the keys alone, use the Keys property.
Dictionary<string, string>.KeyCollection keyColl =
    openWith.Keys;

// The elements of the KeyCollection are strongly typed
// with the type that was specified for dictionary keys.
Console.WriteLine();
foreach( string s in keyColl )
{
    Console.WriteLine("Key = {0}", s);
}
' To get the keys alone, use the Keys property.
Dim keyColl As _
    Dictionary(Of String, String).KeyCollection = _
    openWith.Keys

' The elements of the KeyCollection are strongly typed
' with the type that was specified for dictionary keys.
Console.WriteLine()
For Each s As String In  keyColl
    Console.WriteLine("Key = {0}", s)
Next s
// 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);
}
// 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 kvp As KeyValuePair(Of String, String) In openWith
    Console.WriteLine("Key = {0}, Value = {1}", _
        kvp.Key, kvp.Value)
Next kvp

備註

Dictionary<TKey,TValue>.KeyCollection索引鍵的順序未指定,但與 屬性所Values傳回的相關聯值Dictionary<TKey,TValue>.ValueCollection的順序相同。

Dictionary<TKey,TValue>.KeyCollection 回的 不是靜態複本, Dictionary<TKey,TValue>.KeyCollection 而是會參考原始 Dictionary<TKey,TValue>中的索引鍵。 因此,對的變更 Dictionary<TKey,TValue> 會繼續反映在 中 Dictionary<TKey,TValue>.KeyCollection

取得此屬性的值是 O (1) 作業。

適用於

另請參閱