Dictionary<TKey,TValue>.Keys プロパティ

定義

Dictionary<TKey,TValue> 内のキーを格納しているコレクションを取得します。

public System.Collections.Generic.Dictionary<TKey,TValue>.KeyCollection Keys { get; }

プロパティ値

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();
foreach( string s in keyColl )
{
    Console.WriteLine("Key = {0}", s);
}
// 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);
}

注釈

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) 操作です。

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

こちらもご覧ください