注意:此属性在 .NET Framework 2.0 版中是新增的。
获取包含
SortedDictionary 中的键的集合。
命名空间:System.Collections.Generic
程序集:System(在 system.dll 中)
Public ReadOnly Property Keys As KeyCollection
Dim instance As SortedDictionary(Of TKey, TValue)
Dim value As KeyCollection
value = instance.Keys
public KeyCollection Keys { get; }
public:
property KeyCollection^ Keys {
KeyCollection^ get ();
}
/** @property */
public KeyCollection get_Keys ()
public function get Keys () : KeyCollection
属性值
包含 SortedDictionary 中的键的 SortedDictionary.KeyCollection。
SortedDictionary.KeyCollection 中的键根据 Comparer 属性进行排序,并且与 Values 属性所返回的 SortedDictionary.ValueCollection 中的关联值具有相同的顺序。
返回的 SortedDictionary.KeyCollection 不是静态副本;相反,SortedDictionary.KeyCollection 反向引用原始 SortedDictionary 中的键。因此,对 SortedDictionary 的更改将继续反映到 SortedDictionary.KeyCollection 中。
获取此属性的值的运算复杂度为 O(1)。
下面的代码示例演示如何使用 Keys 属性枚举字典中的键,并演示如何枚举字典中的键和值。
此代码是可以编译和执行的更大示例的一部分。请参见 SortedDictionary。
' To get the keys alone, use the Keys property.
Dim keyColl _
As SortedDictionary(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
<br /><span space="preserve">...</span><br /> ' 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
// To get the keys alone, use the Keys property.
SortedDictionary<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);
}
<br /><span space="preserve">...</span><br /> // 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);
}
Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
.NET Framework
受以下版本支持:2.0