KeyedCollection<'TKey, 'TItem>.RemoveItem Method (Int32)

 

Removes the element at the specified index of the KeyedCollection<'TKey, 'TItem>.

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

override RemoveItem : 
        index:int -> unit

Parameters

index
Type: System.Int32

The index of the element to remove.

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.

This method is an O(n) operation, where n is Count.

Override this method to provide customized behavior for the Remove('T) and RemoveAt(Int32) methods, inherited from the Collection<'T> generic class, and the Remove('TKey) method.

Call the base class implementation of this method to remove the item from the underlying collection and to update the lookup dictionary.

This section contains two code examples that demonstrate overriding the RemoveItem method to provide custom behavior for removing objects from the collection. The first example adds a custom notification event and the second provides support for a collection of objects with mutable keys.

Example 1

The following code example shows how to override the protected InsertItem, RemoveItem, ClearItems, and SetItem methods, to provide custom behavior for the Add, Remove, and Clear methods, and for setting the default Item property (the indexer in C#). The custom behavior provided in this example is a notification event named Changed, which is raised at the end of each of the overridden methods.

The code example creates the SimpleOrder class, which derives from KeyedCollection<'TKey, 'TItem> and represents a simple order form. The order form contains OrderItem objects representing items ordered. The code example also creates a SimpleOrderChangedEventArgs class to contain the event information, and an enumeration to identify the type of change.

The code example demonstrates the custom behavior by calling the properties and methods of the derived class, in the Main method of the Demo class.

This code example uses objects with immutable keys. For a code example that uses mutable keys, see ChangeItemKey.

No code example is currently available or this language may not be supported.

Example 2

The following code example shows how to override the protected ChangeItemKey method to support mutable keys, and how to override the protected InsertItem, RemoveItem, ClearItems, and SetItem methods to maintain the integrity of the keys and the collection.

The code example creates the MutableKeys collection, which derives from KeyedCollection<'TKey, 'TItem>, and the MutableKey class. The MutableKey class has a settable Key property. When a new key is assigned to the property, the property setter calls the internal (Friend in Visual Basic) ChangeKey method of the collection to test whether the new key would conflict with an existing key. If so, an exception is thrown and the property value is not changed.

In order to maintain the connection between a MutableKey object and the MutableKeys collection and to prevent an object from being inserted into two collections, the MutableKey class has an internal (Friend in Visual Basic) Collection field. This field is maintained by the protected methods that provide custom behavior for adding and removing items from the collection, such as the InsertItem method. The field is set when the item is added to a collection and cleared when the item is removed.

No code example is currently available or this language may not be supported.

Universal Windows Platform
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
Return to top
Show: