KeyedCollection<'TKey, 'TItem>.Contains Method ('TKey)
Determines whether the collection contains an element with the specified key.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- key
-
Type:
TKey
The key to locate in the KeyedCollection<'TKey, 'TItem>.
Return Value
Type: System.Booleantrue if the KeyedCollection<'TKey, 'TItem> contains an element with the specified key; otherwise, false.
| Exception | Condition |
|---|---|
| ArgumentNullException | key is null. |
If the KeyedCollection<'TKey, 'TItem> has a lookup dictionary, key is used to search the dictionary. If there is no lookup dictionary, the key of each element is extracted using the GetKeyForItem method and compared with the specified key.
This method is an O(1) operation if the KeyedCollection<'TKey, 'TItem> has a lookup dictionary; otherwise it is an O(n) operation, where n is Count.
This code example shows the minimum code necessary to derive a collection class from KeyedCollection<'TKey, 'TItem>: overriding the GetKeyForItem method and providing a public constructor that delegates to a base class constructor. The code example also demonstrates many of the properties and methods inherited from KeyedCollection<'TKey, 'TItem> and Collection<'T> classes.
The SimpleOrder class is a very simple requisition list that contains OrderItem objects, each of which represents a line item in the order. The key of OrderItem is immutable, an important consideration for classes that derive from KeyedCollection<'TKey, 'TItem>. For a code example that uses mutable keys, see ChangeItemKey.
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1