ListDictionary.Values属性用于获取包含ListDictionary中的值的ICollection。
句法:
public System.Collections.ICollection Values { get; }
返回值:返回一个ICollection,其中包含ListDictionary中的值。
下面的程序说明了ListDictionary.Values属性的用法:
范例1:
// C# code to get an ICollection
// containing the values in the ListDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
// Driver code
public static void Main()
{
// Creating a ListDictionary named myDict
ListDictionary myDict = new ListDictionary();
myDict.Add("Australia", "Canberra");
myDict.Add("Belgium", "Brussels");
myDict.Add("Netherlands", "Amsterdam");
myDict.Add("China", "Beijing");
myDict.Add("Russia", "Moscow");
myDict.Add("India", "New Delhi");
// Get an ICollection containing
// the values in the ListDictionary
ICollection ic = myDict.Values;
// Displaying the values in ICollection ic
foreach(String str in ic)
{
Console.WriteLine(str);
}
}
}
输出:
Canberra
Brussels
Amsterdam
Beijing
Moscow
New Delhi
范例2:
// C# code to get an ICollection
// containing the values in the ListDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
// Driver code
public static void Main()
{
// Creating a ListDictionary named myDict
ListDictionary myDict = new ListDictionary();
myDict.Add("I", "first");
myDict.Add("II", "second");
myDict.Add("III", "third");
myDict.Add("IV", "fourth");
myDict.Add("V", "fifth");
// Get an ICollection containing
// the values in the ListDictionary
ICollection ic = myDict.Values;
// Displaying the values in ICollection ic
foreach(String str in ic)
{
Console.WriteLine(str);
}
}
}
输出:
first
second
third
fourth
fifth
笔记:
- ICollection中值的顺序未指定,但与Keys方法返回的ICollection中的关联键相同。
- 返回的ICollection不是静态副本。而是,ICollection引用了原始ListDictionary中的值。因此,对ListDictionary的更改将继续反映在ICollection中。
- 检索此属性的值是O(1)操作。
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.specialized.listdictionary.values?view=netframework-4.7.2