KeyedCollection<'TKey, 'TItem>.Remove Method ('TKey)
Removes the element with the specified key from the KeyedCollection<'TKey, 'TItem>.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- key
-
Type:
TKey
The key of the element to remove.
Return Value
Type: System.Booleantrue if the element is successfully removed; otherwise, false. This method also returns false if key is not found in the KeyedCollection<'TKey, 'TItem>.
| Exception | Condition |
|---|---|
| ArgumentNullException | key is null. |
The key of the element is also removed from the lookup dictionary.
If the number of elements has exceeded the dictionary creation threshold and the KeyedCollection<'TKey, 'TItem> is using a lookup dictionary, it will continue to use a lookup dictionary even though the number of elements is again under the threshold.
Note |
|---|
To customize the behavior of this method, override the RemoveItem method. |
This method 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 example demonstrates Remove method inherited from KeyedCollection<'TKey, 'TItem>, which removes the item with a specified key, and also the Remove and RemoveAt methods inherited from Collection<'T>, which remove by object and by index respectively.
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
