Dictionary<TKey,TValue>.Values 屬性

定義

取得集合,包含 Dictionary<TKey,TValue> 中的值。

C#
public System.Collections.Generic.Dictionary<TKey,TValue>.ValueCollection Values { get; }

屬性值

Dictionary<TKey,TValue>.ValueCollection,包含 Dictionary<TKey,TValue> 中的值。

範例

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

此程式代碼範例是針對 Dictionary<TKey,TValue> 類別提供的較大範例的一部分, openWith (是本範例) 中使用的 Dictionary 名稱。

C#
// To get the values alone, use the Values property.
Dictionary<string, string>.ValueCollection valueColl =
    openWith.Values;

// The elements of the ValueCollection are strongly typed
// with the type that was specified for dictionary values.
Console.WriteLine();
foreach( string s in valueColl )
{
    Console.WriteLine("Value = {0}", s);
}
C#
// 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>.ValueCollection 中值的順序並未指定,但此順序會與 Dictionary<TKey,TValue>.KeyCollection 屬性傳回之 Keys 中關聯索引鍵的順序相同。

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

取得此屬性的值是 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

另請參閱