HybridDictionary.Keys属性用于获取包含HybridDictionary中的键的ICollection。
句法:
public System.Collections.ICollection Keys { get; }
返回值:返回包含HybridDictionary中的键的ICollection。
下面的程序说明了HybridDictionary.Keys属性的用法:
范例1:
// C# code to get an ICollection containing
// the keys in the HybridDictionary.
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
// Driver code
public static void Main()
{
// Creating a HybridDictionary named myDict
HybridDictionary myDict = new HybridDictionary();
// Adding key/value pairs in myDict
myDict.Add("A", "Apple");
myDict.Add("B", "Banana");
myDict.Add("C", "Cat");
myDict.Add("D", "Dog");
myDict.Add("E", "Elephant");
myDict.Add("F", "Fish");
// Creating a String arr named myArr
String[] myArr = new String[myDict.Count];
// copying the Keys in HybridDictionary
// to a one-dimensional Array instance
// at the specified index.
myDict.Keys.CopyTo(myArr, 0);
// To get an ICollection containing
// the keys in the HybridDictionary
for (int i = 0; i < myDict.Count; i++)
Console.WriteLine(myArr[i]);
}
}
输出:
A
B
C
D
E
F
范例2:
// C# code to get an ICollection containing
// the keys in the HybridDictionary.
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
// Driver code
public static void Main()
{
// Creating a HybridDictionary named myDict
HybridDictionary myDict = new HybridDictionary();
// Adding key/value pairs in myDict
myDict.Add("I", "first");
myDict.Add("II", "second");
myDict.Add("III", "third");
myDict.Add("IV", "fourth");
myDict.Add("V", "fifth");
// Creating a String arr named myArr
String[] myArr = new String[myDict.Count];
// copying the Keys in HybridDictionary
// to a one-dimensional Array instance
// at the specified index.
myDict.Keys.CopyTo(myArr, 0);
// To get an ICollection containing
// the keys in the HybridDictionary
for (int i = 0; i < myDict.Count; i++)
Console.WriteLine(myArr[i]);
}
}
输出:
I
II
III
IV
V
笔记:
- ICollection中值的顺序未指定,但与Values方法返回的ICollection中关联值的顺序相同。
- 返回的ICollection不是静态副本。而是,ICollection引用了原始HybridDictionary中的键。因此,对HybridDictionary的更改将继续反映在ICollection中。
- 检索此属性的值是O(1)操作。
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.specialized.hybriddictionary.keys?view=netframework-4.7.2