In analyzing this class, my take is that it is totally irrelevant. Use the base class KeyedCollection
and write the GetKeyForItem
method to return the Type of the Item parameter.
There are four methods defined on the KeyedByTypeCollection
that are irrelevant (Find
, FindAll
, Remove
, and RemoveAll
). Find, FindAll, and RemoveAll have no base KeyedCollection
implementations. Remove does exist in the base class.
The first problem with all four of these methods in the KeyedByTypeCollection
is that they perform a serial search of the base Collection to find the item(s). Why not just use the indexer from KeyedCollection
to retrieve the appropriate item?
Second, FindAll
and RemoveAll
will only ever find 1 item (if one exists in the Collection) - because BY DEFINITION, only one item of the specified Type will exist in the Collection (oh, and FindAll
will serially read through all the elements in the Collection, even if it finds the matching element at the beginning). So, FindAll
and RemoveAll
will read ALL the items in the Collection before either Finding/Removing 0 or 1 item(s). Again, why not use the indexer or the base version of Remove to perform the required process?
The second problem with the class is that it is not designed to be a base class (in other words, don't inherit from it unless you know how the internals of the methods are written). There are no calls to the GetKeyForItem
method in places where it should be used, so, if you override the GetKeyForItem
method, you must also override several other methods to properly get the key for the Collection.