StringDictionary.ContainsValue(String) 方法

定义

确定 StringDictionary 是否包含特定值。

public:
 virtual bool ContainsValue(System::String ^ value);
public virtual bool ContainsValue (string value);
public virtual bool ContainsValue (string? value);
abstract member ContainsValue : string -> bool
override this.ContainsValue : string -> bool
Public Overridable Function ContainsValue (value As String) As Boolean

参数

value
String

要在 StringDictionary 中定位的值。 该值可以为 null

返回

如果 true 包含具有指定值的元素,则为 StringDictionary;否则为 false

示例

下面的代码示例在 中 StringDictionary搜索 元素。

#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
void PrintKeysAndValues( StringDictionary^ myCol )
{
   Console::WriteLine( "   KEY        VALUE" );
   IEnumerator^ enum0 = myCol->GetEnumerator();
   while ( enum0->MoveNext() )
   {
      DictionaryEntry^ de = safe_cast<DictionaryEntry^>(enum0->Current);
      Console::WriteLine( "   {0,-10} {1}", de->Key, de->Value );
   }

   Console::WriteLine();
}

int main()
{
   
   // Creates and initializes a new StringDictionary.
   StringDictionary^ myCol = gcnew StringDictionary;
   myCol->Add( "red", "rojo" );
   myCol->Add( "green", "verde" );
   myCol->Add( "blue", "azul" );
   
   // Displays the values in the StringDictionary.
   Console::WriteLine( "Initial contents of the StringDictionary:" );
   PrintKeysAndValues( myCol );
   
   // Searches for a key.
   if ( myCol->ContainsKey( "red" ) )
      Console::WriteLine( "The collection contains the key \"red\"." );
   else
      Console::WriteLine( "The collection does not contain the key \"red\"." );

   Console::WriteLine();
   
   // Searches for a value.
   if ( myCol->ContainsValue( "amarillo" ) )
      Console::WriteLine( "The collection contains the value \"amarillo\"." );
   else
      Console::WriteLine( "The collection does not contain the value \"amarillo\"." );

   Console::WriteLine();
}

/*
This code produces the following output.

Initial contents of the StringDictionary:
   KEY        VALUE
   green      verde
   red        rojo
   blue       azul

The collection contains the key "red".

The collection does not contain the value "amarillo".

*/
using System;
using System.Collections;
using System.Collections.Specialized;

public class SamplesStringDictionary  {

   public static void Main()  {

      // Creates and initializes a new StringDictionary.
      StringDictionary myCol = new StringDictionary();
      myCol.Add( "red", "rojo" );
      myCol.Add( "green", "verde" );
      myCol.Add( "blue", "azul" );

      // Displays the values in the StringDictionary.
      Console.WriteLine( "Initial contents of the StringDictionary:" );
      PrintKeysAndValues( myCol );

      // Searches for a key.
      if ( myCol.ContainsKey( "red" ) )
         Console.WriteLine( "The collection contains the key \"red\"." );
      else
         Console.WriteLine( "The collection does not contain the key \"red\"." );
      Console.WriteLine();

      // Searches for a value.
      if ( myCol.ContainsValue( "amarillo" ) )
         Console.WriteLine( "The collection contains the value \"amarillo\"." );
      else
         Console.WriteLine( "The collection does not contain the value \"amarillo\"." );
      Console.WriteLine();
   }

   public static void PrintKeysAndValues( StringDictionary myCol )  {
      Console.WriteLine( "   KEY        VALUE" );
      foreach ( DictionaryEntry de in myCol )
         Console.WriteLine( "   {0,-10} {1}", de.Key, de.Value );
      Console.WriteLine();
   }
}

/*
This code produces the following output.

Initial contents of the StringDictionary:
   KEY        VALUE
   green      verde
   red        rojo
   blue       azul

The collection contains the key "red".

The collection does not contain the value "amarillo".

*/
Imports System.Collections
Imports System.Collections.Specialized

Public Class SamplesStringDictionary   

   Public Shared Sub Main()

      ' Creates and initializes a new StringDictionary.
      Dim myCol As New StringDictionary()
      myCol.Add("red", "rojo")
      myCol.Add("green", "verde")
      myCol.Add("blue", "azul")

      ' Displays the values in the StringDictionary.
      Console.WriteLine("Initial contents of the StringDictionary:")
      PrintKeysAndValues(myCol)

      ' Searches for a key.
      If myCol.ContainsKey("red") Then
         Console.WriteLine("The collection contains the key ""red"".")
      Else
         Console.WriteLine("The collection does not contain the key ""red"".")
      End If
      Console.WriteLine()

      ' Searches for a value.
      If myCol.ContainsValue("amarillo") Then
         Console.WriteLine("The collection contains the value ""amarillo"".")
      Else
         Console.WriteLine("The collection does not contain the value ""amarillo"".")
      End If
      Console.WriteLine()

   End Sub

   Public Shared Sub PrintKeysAndValues(myCol As StringDictionary)
      Dim de As DictionaryEntry

      Console.WriteLine("   KEY        VALUE")
      For Each de In  myCol
         Console.WriteLine("   {0,-10} {1}", de.Key, de.Value)
      Next de
      Console.WriteLine()

   End Sub

End Class


'This code produces the following output.
'
'Initial contents of the StringDictionary:
'   KEY        VALUE
'   green      verde
'   red        rojo
'   blue       azul
'
'The collection contains the key "red".
'
'The collection does not contain the value "amarillo".

注解

使用 Object.Equals 方法将 StringDictionary 元素的值与指定的值进行比较。

此方法执行线性搜索;因此,平均执行时间与 Count成正比。 也就是说,此方法是 O (n) 操作,其中 nCount

从 .NET Framework 2.0 开始,此方法使用 集合的对象 EqualsCompareTo 方法item来确定是否存在item。 在早期版本的 .NET Framework中,此确定是通过对集合中的 对象使用 Equals 参数的 itemCompareTo 方法做出的。

适用于

另请参阅