System.Collections.Hashtable.ContainsValue 方法

方法描述

确定 Hashtable 是否包含特定值。

语法定义(C# System.Collections.Hashtable.ContainsValue 方法 的用法)

public virtual bool ContainsValue(
	Object value
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
value System-Object 要在 Hashtable 中定位的值。该值可以为 null。
返回值 System.Boolean 如果 Hashtable 包含带有指定的 value 的元素,则为 true;否则为 false。

提示和注释

使用 Object.Equals 方法将 Hashtable 的元素的值与指定的值相比较。

此方法执行线性搜索;因此,此方法的运算复杂度是 O(n),其中 n 是 Count。

从 .NET Framework 2.0 开始,此方法对 item 使用该集合对象的 Equals 和 CompareTo 方法来确定项是否存在。 在 .NET Framework 的较早版本中,这是通过对集合中的对象使用 item 参数的 Equals 和 CompareTo 方法来确定的。

System.Collections.Hashtable.ContainsValue 方法例子

下面的示例说明如何确定 Hashtable 是否包含特定的元素。

using System;
using System.Collections;
public class SamplesHashtable  {

   public static void Main()  {

      // Creates and initializes a new Hashtable.
      Hashtable myHT = new Hashtable();
      myHT.Add( 0, "zero" );
      myHT.Add( 1, "one" );
      myHT.Add( 2, "two" );
      myHT.Add( 3, "three" );
      myHT.Add( 4, "four" );

      // Displays the values of the Hashtable.
      Console.WriteLine( "The Hashtable contains the following values:" );
      PrintIndexAndKeysAndValues( myHT );

      // Searches for a specific key.
      int myKey = 2;
      Console.WriteLine( "The key \"{0}\" is {1}.", myKey, myHT.ContainsKey( myKey ) ? "in the Hashtable" : "NOT in the Hashtable" );
      myKey = 6;
      Console.WriteLine( "The key \"{0}\" is {1}.", myKey, myHT.ContainsKey( myKey ) ? "in the Hashtable" : "NOT in the Hashtable" );

      // Searches for a specific value.
      String myValue = "three";
      Console.WriteLine( "The value \"{0}\" is {1}.", myValue, myHT.ContainsValue( myValue ) ? "in the Hashtable" : "NOT in the Hashtable" );
      myValue = "nine";
      Console.WriteLine( "The value \"{0}\" is {1}.", myValue, myHT.ContainsValue( myValue ) ? "in the Hashtable" : "NOT in the Hashtable" );
   }


   public static void PrintIndexAndKeysAndValues( Hashtable myHT )  {
      int i = 0;
      Console.WriteLine( "\t-INDEX-\t-KEY-\t-VALUE-" );
      foreach ( DictionaryEntry de in myHT )
         Console.WriteLine( "\t[{0}]:\t{1}\t{2}", i++, de.Key, de.Value );
      Console.WriteLine();
   }

}


/* 
This code produces the following output.

The Hashtable contains the following values:
        -INDEX- -KEY-   -VALUE-
        [0]:    4       four
        [1]:    3       three
        [2]:    2       two
        [3]:    1       one
        [4]:    0       zero

The key "2" is in the Hashtable.
The key "6" is NOT in the Hashtable.
The value "three" is in the Hashtable.
The value "nine" is NOT in the Hashtable.

*/

异常

异常 异常描述

命名空间

namespace: System.Collections

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。