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

同时提供下列产品的其他版本:
.NET Framework 类库
Dictionary.ContainsKey 方法

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

确定 Dictionary 是否包含指定的键。

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

Visual Basic(声明)
Public Function ContainsKey ( _
    key As TKey _
) As Boolean
Visual Basic(用法)
Dim instance As Dictionary(Of TKey, TValue)
Dim key As TKey
Dim returnValue As Boolean

returnValue = instance.ContainsKey(key)
C#
public bool ContainsKey (
    TKey key
)
C++
public:
virtual bool ContainsKey (
    TKey key
) sealed
J#
public final boolean ContainsKey (
    TKey key
)
JScript
public final function ContainsKey (
    key : TKey
) : boolean

参数

key

要在 Dictionary 中定位的键。

返回值

如果 Dictionary 包含具有指定键的元素,则为 true;否则为 false
异常类型条件

ArgumentNullException

key 为 空引用(在 Visual Basic 中为 Nothing)。

此方法的运算复杂度接近 O(1)。

下面的代码示例演示如何在调用 Add 方法之前使用 ContainsKey 方法测试某个键是否存在。它还演示如何使用 TryGetValue 方法来检索值,当程序频繁尝试字典中不存在的键时,这种方法是一种更有效的检索值的方法。最后,它还演示用于测试键是否存在的一种效率最低的方法,即使用 Item 属性(在 C# 中为索引器)。

此代码示例摘自一个为 Dictionary 类提供的更大的示例。

Visual Basic
        ' ContainsKey can be used to test keys before inserting 
        ' them.
        If Not openWith.ContainsKey("ht") Then
            openWith.Add("ht", "hypertrm.exe")
            Console.WriteLine("Value added for key = ""ht"": {0}", _
                openWith("ht"))
        End If
<br /><span space="preserve">...</span><br />        ' When a program often has to try keys that turn out not to
        ' be in the dictionary, TryGetValue can be a more efficient 
        ' way to retrieve values.
        Dim value As String = ""
        If openWith.TryGetValue("tif", value) Then
            Console.WriteLine("For key = ""tif"", value = {0}.", value)
        Else
            Console.WriteLine("Key = ""tif"" is not found.")
        End If
<br /><span space="preserve">...</span><br />        ' The default Item property throws an exception if the requested
        ' key is not in the dictionary.
        Try
            Console.WriteLine("For key = ""tif"", value = {0}.", _
                openWith("tif"))
        Catch 
            Console.WriteLine("Key = ""tif"" is not found.")
        End Try
C#
        // ContainsKey can be used to test keys before inserting 
        // them.
        if (!openWith.ContainsKey("ht"))
        {
            openWith.Add("ht", "hypertrm.exe");
            Console.WriteLine("Value added for key = \"ht\": {0}", 
                openWith["ht"]);
        }
<br /><span space="preserve">...</span><br />        // When a program often has to try keys that turn out not to
        // be in the dictionary, TryGetValue can be a more efficient 
        // way to retrieve values.
        string value = "";
        if (openWith.TryGetValue("tif", out value))
        {
            Console.WriteLine("For key = \"tif\", value = {0}.", value);
        }
        else
        {
            Console.WriteLine("Key = \"tif\" is not found.");
        }
<br /><span space="preserve">...</span><br />        // The indexer throws an exception if the requested key is
        // not in the dictionary.
        try
        {
            Console.WriteLine("For key = \"tif\", value = {0}.", 
                openWith["tif"]);
        }
        catch (KeyNotFoundException)
        {
            Console.WriteLine("Key = \"tif\" is not found.");
        }

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