Export (0) Print
Expand All

Dictionary(Of TKey, TValue).ICollection(Of KeyValuePair(Of TKey, TValue)).Contains Method

Determines whether the ICollection(Of T) contains a specific key and value.

Namespace:  System.Collections.Generic
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Private Function Contains ( _
	keyValuePair As KeyValuePair(Of TKey, TValue) _
) As Boolean Implements ICollection(Of KeyValuePair(Of TKey, TValue)).Contains

Parameters

keyValuePair
Type: System.Collections.Generic.KeyValuePair(Of TKey, TValue)

The KeyValuePair(Of TKey, TValue) structure to locate in the ICollection(Of T).

Return Value

Type: System.Boolean
true if keyValuePair is found in the ICollection(Of T); otherwise, false.

Implements

ICollection(Of T).Contains(T)

This method determines equality using the default equality comparer EqualityComparer(Of T).Default for the System.Collections.Generic.KeyValuePair(Of TKey, TValue) structure.

This method approaches an O(1) operation.

The following example shows how to use the ICollection(Of KeyValuePair(Of TKey, TValue)).Add, ICollection(Of KeyValuePair(Of TKey, TValue)).Contains, ICollection(Of KeyValuePair(Of TKey, TValue)).CopyTo, and ICollection(Of KeyValuePair(Of TKey, TValue)).Remove methods of the System.Collections.Generic.ICollection(Of T) generic interface to manipulate a Dictionary(Of TKey, TValue) object.

Imports System
Imports System.Collections.Generic

Public Class Example

    Public Shared Sub Main() 

        ' Create a new dictionary of strings, with string keys, and 
        ' access it through the generic ICollection interface. The 
        ' generic ICollection interface views the dictionary as a 
        ' collection of KeyValuePair objects with the same type 
        ' arguments as the dictionary. 
        
        Dim openWith As ICollection(Of KeyValuePair(Of String, String)) _
            = New Dictionary(Of String, String)

        ' Add some elements to the dictionary. When elements are  
        ' added through the ICollection(Of T) interface, the keys 
        ' and values must be wrapped in KeyValuePair objects. 
        '
        openWith.Add(New KeyValuePair(Of String,String)("txt", "notepad.exe"))
        openWith.Add(New KeyValuePair(Of String,String)("bmp", "paint.exe"))
        openWith.Add(New KeyValuePair(Of String,String)("dib", "paint.exe"))
        openWith.Add(New KeyValuePair(Of String,String)("rtf", "wordpad.exe"))

        Console.WriteLine()
        For Each element As KeyValuePair(Of String, String) in openWith
            Console.WriteLine("{0}, {1}", element.Key, element.Value)
        Next 

        ' The Contains method also takes a KeyValuePair object. 
        '
        Console.WriteLine(vbLf & _
            "Contains(KeyValuePair(""txt"", ""notepad.exe"")): {0}", _
            openWith.Contains(New KeyValuePair(Of String,String)("txt", "notepad.exe")))

        ' The Remove method takes a KeyValuePair object.) 
        
        ' Use the Remove method to remove a key/value pair.
        Console.WriteLine(vbLf &  _
            "Remove(New KeyValuePair(""dib"", ""paint.exe""))")
        openWith.Remove(New KeyValuePair(Of String,String)("dib", "paint.exe"))

        Console.WriteLine()
        For Each element As KeyValuePair(Of String, String) in openWith
            Console.WriteLine("{0}, {1}", element.Key, element.Value)
        Next 

        ' Create an array of KeyValuePair objects and copy the  
        ' contents of the dictionary to it. Subtract one from the 
        ' array size because Visual Basic allocates an extra array 
        ' element. 
        Dim copy(openWith.Count - 1) As KeyValuePair(Of String, String)
        openWith.CopyTo(copy, 0)

        ' List the contents of the array. 
        '
        Console.WriteLine()
        For Each element As KeyValuePair(Of String, String) in copy
            Console.WriteLine("{0}, {1}", element.Key, element.Value)
        Next 

    End Sub 

End Class 

' This code example produces the following output: 

'txt, notepad.exe 
'bmp, paint.exe 
'dib, paint.exe 
'rtf, wordpad.exe 

'Contains(KeyValuePair("txt", "notepad.exe")): True 

'Remove(New KeyValuePair("dib", "paint.exe")) 

'txt, notepad.exe 
'bmp, paint.exe 
'rtf, wordpad.exe 

'txt, notepad.exe 
'bmp, paint.exe 
'rtf, wordpad.exe 

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft