SortedDictionary<TKey,TValue>.IDictionary.Contains(Object) Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Avgör om innehåller IDictionary ett element med den angivna nyckeln.
virtual bool System.Collections.IDictionary.Contains(System::Object ^ key) = System::Collections::IDictionary::Contains;
bool IDictionary.Contains(object key);
abstract member System.Collections.IDictionary.Contains : obj -> bool
override this.System.Collections.IDictionary.Contains : obj -> bool
Function Contains (key As Object) As Boolean Implements IDictionary.Contains
Parametrar
- key
- Object
Nyckeln som ska hittas IDictionaryi .
Returer
true
IDictionary om innehåller ett element med nyckeln, annars . false
Implementeringar
Undantag
key är null.
Exempel
I följande kodexempel visas hur du använder IDictionary.Contains -metoden för System.Collections.IDictionary gränssnittet med en SortedDictionary<TKey,TValue>. Exemplet visar att metoden returnerar false om en nyckel av fel datatyp anges.
Kodexemplet är en del av ett större exempel, inklusive utdata, som tillhandahålls IDictionary.Add för metoden.
using System;
using System.Collections;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
// Create a new sorted dictionary of strings, with string keys,
// and access it using the IDictionary interface.
//
IDictionary openWith = new SortedDictionary<string, string>();
// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
// IDictionary.Add throws an exception if incorrect types
// are supplied for key or value.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
Imports System.Collections
Imports System.Collections.Generic
Public Class Example
Public Shared Sub Main()
' Create a new sorted dictionary of strings, with string keys,
' and access it using the IDictionary interface.
'
Dim openWith As IDictionary = _
New SortedDictionary(Of String, String)
' Add some elements to the dictionary. There are no
' duplicate keys, but some of the values are duplicates.
' IDictionary.Add throws an exception if incorrect types
' are supplied for key or value.
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("dib", "paint.exe")
openWith.Add("rtf", "wordpad.exe")
// Contains can be used to test keys before inserting
// them.
if (!openWith.Contains("ht"))
{
openWith.Add("ht", "hypertrm.exe");
Console.WriteLine("Value added for key = \"ht\": {0}",
openWith["ht"]);
}
// IDictionary.Contains returns false if the wrong data
// type is supplied.
Console.WriteLine("openWith.Contains(29.7) returns {0}",
openWith.Contains(29.7));
' Contains can be used to test keys before inserting
' them.
If Not openWith.Contains("ht") Then
openWith.Add("ht", "hypertrm.exe")
Console.WriteLine("Value added for key = ""ht"": {0}", _
openWith("ht"))
End If
' IDictionary.Contains returns False if the wrong data
' type is supplied.
Console.WriteLine("openWith.Contains(29.7) returns {0}", _
openWith.Contains(29.7))
}
}
End Sub
End Class
Kommentarer
Den här metoden returnerar false om key är av en typ som inte kan tilldelas till nyckeltypen TKey för SortedDictionary<TKey,TValue>.
Den här metoden är en O(log n)-åtgärd, där n är Count.