This documentation is archived and is not being maintained.

Dictionary.System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>.Contains Method 

Note: This method is new in the .NET Framework version 2.0.

Determines whether the ICollection contains a specific key and value.

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

bool ICollection<KeyValuePair<TKey,TValue>>.Contains (
	KeyValuePair<TKey,TValue> keyValuePair
)
J# supports the use of explicit interface implementations, but not the declaration of new ones.
JScript supports the use of explicit interface implementations, but not the declarations of new ones.

Parameters

keyValuePair

The KeyValuePair structure to locate in the ICollection.

Return Value

true if keyValuePair is found in the ICollection; otherwise, false.

This method determines equality using the default equality comparer EqualityComparer.Default for the System.Collections.Generic.KeyValuePair structure.

This method approaches an O(1) operation.

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

using System;
using System.Collections.Generic;

public class Example
{
    public static void 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.
        //
        ICollection<KeyValuePair<String, String>> openWith =
            new Dictionary<String, String>();
        
        // Add some elements to the dictionary. When elements are 
        // added through the ICollection<T> interface, the keys
        // and values must be wrapped in KeyValuePair objects.
        //
        openWith.Add(new KeyValuePair<String,String>("txt", "notepad.exe"));
        openWith.Add(new KeyValuePair<String,String>("bmp", "paint.exe"));
        openWith.Add(new KeyValuePair<String,String>("dib", "paint.exe"));
        openWith.Add(new KeyValuePair<String,String>("rtf", "wordpad.exe"));
        
        Console.WriteLine();
        foreach( KeyValuePair<string, string> element in openWith )
        {
            Console.WriteLine("{0}, {1}", element.Key, element.Value);
        }
           
        // The Contains method also takes a KeyValuePair object.
        //
        Console.WriteLine(
            "\nContains(KeyValuePair(\"txt\", \"notepad.exe\")): {0}", 
            openWith.Contains(new KeyValuePair<String,String>("txt", "notepad.exe")));

        // The Remove method takes a KeyValuePair object.)
        //
        // Use the Remove method to remove a key/value pair.
        Console.WriteLine("\nRemove(new KeyValuePair(\"dib\", \"paint.exe\"))");
        openWith.Remove(new KeyValuePair<String,String>("dib", "paint.exe"));
        
        Console.WriteLine();
        foreach( KeyValuePair<string, string> element in openWith )
        {
            Console.WriteLine("{0}, {1}", element.Key, element.Value);
        }

        // Create an array of KeyValuePair objects and copy the 
        // contents of the dictionary to it. 
        // 
        KeyValuePair<string, string>[] copy = 
            new KeyValuePair<string, string>[openWith.Count];
        openWith.CopyTo(copy, 0);
    
        // List the contents of the array.
        //
        Console.WriteLine();
        foreach( KeyValuePair<string, string> element in copy )
        {
            Console.WriteLine("{0}, {1}", element.Key, element.Value);
        }
    }
}

/* 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
 */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0

.NET Compact Framework

Supported in: 2.0
Show: