请单击以进行评分并提供反馈
MSDN
MSDN Library
.NET 开发
先前版本
 Keys 属性
全部折叠/全部展开 全部折叠
此页面仅适用于
Microsoft Visual Studio 2005/.NET Framework 2.0

同时提供下列产品的其他版本:
.NET Framework 类库
Dictionary.Keys 属性

注意:此属性在 .NET Framework 2.0 版中是新增的。

获取包含 Dictionary 中的键的集合。

命名空间:System.Collections.Generic
程序集:mscorlib(在 mscorlib.dll 中)

Visual Basic(声明)
Public ReadOnly Property Keys As KeyCollection
Visual Basic(用法)
Dim instance As Dictionary(Of TKey, TValue)
Dim value As KeyCollection

value = instance.Keys
C#
public KeyCollection Keys { get; }
C++
public:
property KeyCollection^ Keys {
    KeyCollection^ get ();
}
J#
/** @property */
public KeyCollection get_Keys ()
JScript
public function get Keys () : KeyCollection

属性值

包含 Dictionary 中的键的 Dictionary.KeyCollection

未指定 Dictionary.KeyCollection 中键的顺序,但是它与由 Values 属性返回的 Dictionary.KeyCollection 中的相关联值的顺序相同。

返回的 Dictionary.KeyCollection 不是静态副本;相反,Dictionary.KeyCollection 反向引用原始 Dictionary 中的键。因此,对 Dictionary 的更改将继续反映到 Dictionary.KeyCollection 中。

获取此属性的值的运算复杂度为 O(1)。

下面的代码示例演示如何使用 Keys 属性枚举字典中的键,并演示如何枚举字典中的键和值。

此代码是可以编译和执行的更大示例的一部分。请参见 Dictionary

Visual Basic
        ' To get the keys alone, use the Keys property.
        Dim keyColl As _
            Dictionary(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
C#
        // 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);
        }
<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 CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、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

.NET Compact Framework

受以下版本支持:2.0
社区内容   什么是社区内容?
添加新内容 RSS  批注
Processing
© 2010 Microsoft Corporation 版权所有。 保留所有权利 | 商标 | 隐私权声明
Page view tracker