ListDictionary.Contains(Object) Metoda
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Określa, czy element ListDictionary zawiera określony klucz.
public:
virtual bool Contains(System::Object ^ key);
public bool Contains (object key);
abstract member Contains : obj -> bool
override this.Contains : obj -> bool
Public Function Contains (key As Object) As Boolean
Parametry
- key
- Object
Klucz do zlokalizowania w pliku ListDictionary.
Zwraca
true
jeśli element ListDictionary zawiera wpis z określonym kluczem; w przeciwnym razie false
wartość .
Implementuje
Wyjątki
key
to null
.
Przykłady
Poniższy przykład kodu wyszukuje element w elemecie ListDictionary.
#using <System.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
void PrintKeysAndValues( IDictionary^ myCol )
{
Console::WriteLine( " KEY VALUE" );
IEnumerator^ myEnum = myCol->GetEnumerator();
while ( myEnum->MoveNext() )
{
DictionaryEntry de = safe_cast<DictionaryEntry>(myEnum->Current);
Console::WriteLine( " {0,-25} {1}", de.Key, de.Value );
}
Console::WriteLine();
}
int main()
{
// Creates and initializes a new ListDictionary.
ListDictionary^ myCol = gcnew ListDictionary;
myCol->Add( "Braeburn Apples", "1.49" );
myCol->Add( "Fuji Apples", "1.29" );
myCol->Add( "Gala Apples", "1.49" );
myCol->Add( "Golden Delicious Apples", "1.29" );
myCol->Add( "Granny Smith Apples", "0.89" );
myCol->Add( "Red Delicious Apples", "0.99" );
// Displays the values in the ListDictionary in three different ways.
Console::WriteLine( "Initial contents of the ListDictionary:" );
PrintKeysAndValues( myCol );
// Searches for a key.
if ( myCol->Contains( "Kiwis" ) )
Console::WriteLine( "The collection contains the key \"Kiwis\"." );
else
Console::WriteLine( "The collection does not contain the key \"Kiwis\"." );
Console::WriteLine();
}
/*
This code produces the following output.
Initial contents of the ListDictionary:
KEY VALUE
Braeburn Apples 1.49
Fuji Apples 1.29
Gala Apples 1.49
Golden Delicious Apples 1.29
Granny Smith Apples 0.89
Red Delicious Apples 0.99
The collection does not contain the key "Kiwis".
*/
using System;
using System.Collections;
using System.Collections.Specialized;
public class SamplesListDictionary {
public static void Main() {
// Creates and initializes a new ListDictionary.
ListDictionary myCol = new ListDictionary();
myCol.Add( "Braeburn Apples", "1.49" );
myCol.Add( "Fuji Apples", "1.29" );
myCol.Add( "Gala Apples", "1.49" );
myCol.Add( "Golden Delicious Apples", "1.29" );
myCol.Add( "Granny Smith Apples", "0.89" );
myCol.Add( "Red Delicious Apples", "0.99" );
// Displays the values in the ListDictionary in three different ways.
Console.WriteLine( "Initial contents of the ListDictionary:" );
PrintKeysAndValues( myCol );
// Searches for a key.
if ( myCol.Contains( "Kiwis" ) )
Console.WriteLine( "The collection contains the key \"Kiwis\"." );
else
Console.WriteLine( "The collection does not contain the key \"Kiwis\"." );
Console.WriteLine();
}
public static void PrintKeysAndValues( IDictionary myCol ) {
Console.WriteLine( " KEY VALUE" );
foreach ( DictionaryEntry de in myCol )
Console.WriteLine( " {0,-25} {1}", de.Key, de.Value );
Console.WriteLine();
}
}
/*
This code produces the following output.
Initial contents of the ListDictionary:
KEY VALUE
Braeburn Apples 1.49
Fuji Apples 1.29
Gala Apples 1.49
Golden Delicious Apples 1.29
Granny Smith Apples 0.89
Red Delicious Apples 0.99
The collection does not contain the key "Kiwis".
*/
Imports System.Collections
Imports System.Collections.Specialized
Public Class SamplesListDictionary
Public Shared Sub Main()
' Creates and initializes a new ListDictionary.
Dim myCol As New ListDictionary()
myCol.Add("Braeburn Apples", "1.49")
myCol.Add("Fuji Apples", "1.29")
myCol.Add("Gala Apples", "1.49")
myCol.Add("Golden Delicious Apples", "1.29")
myCol.Add("Granny Smith Apples", "0.89")
myCol.Add("Red Delicious Apples", "0.99")
' Displays the values in the ListDictionary in three different ways.
Console.WriteLine("Initial contents of the ListDictionary:")
PrintKeysAndValues(myCol)
' Searches for a key.
If myCol.Contains("Kiwis") Then
Console.WriteLine("The collection contains the key ""Kiwis"".")
Else
Console.WriteLine("The collection does not contain the key ""Kiwis"".")
End If
Console.WriteLine()
End Sub
Public Shared Sub PrintKeysAndValues(myCol As IDictionary)
Console.WriteLine(" KEY VALUE")
Dim de As DictionaryEntry
For Each de In myCol
Console.WriteLine(" {0,-25} {1}", de.Key, de.Value)
Next de
Console.WriteLine()
End Sub
End Class
'This code produces the following output.
'
'Initial contents of the ListDictionary:
' KEY VALUE
' Braeburn Apples 1.49
' Fuji Apples 1.29
' Gala Apples 1.49
' Golden Delicious Apples 1.29
' Granny Smith Apples 0.89
' Red Delicious Apples 0.99
'
'The collection does not contain the key "Kiwis".
Uwagi
Ta metoda jest operacją O(n
), gdzie n
to Count.
Począwszy od .NET Framework 2.0, ta metoda używa metod key
i CompareTo obiektów Equals kolekcji do określenia, czy item
istnieje. We wcześniejszych wersjach .NET Framework ta determinacja została wykonana przy użyciu Equals metod item
i CompareTo parametru w obiektach w kolekcji.