Dictionary or KeyedCollection?

后端 未结 6 745
无人及你
无人及你 2020-12-10 00:26

I have a class (SomeClass) which contains a property Name of string type. And I need to store an array of that class and find its item

相关标签:
6条回答
  • 2020-12-10 00:45

    Here is good explanation about differences between Dictionary and KeyedCollection: http://geekswithblogs.net/NewThingsILearned/archive/2010/01/07/using-keyedcollectionlttkey-titemgt.aspx

    Main points are:

    • KeyedCollection is abstract, so you can't use it directly.
    • KeyedCollection is useful for cases, when key is in entity itself, then you can encapsulate key retrieval within collection implementation.
    • There are generic implementations for KeyedCollection (not in the framework though), which allow you to paste key retrieval delegate in collection constructor, so you don't have to repeat it each time you add item.
    0 讨论(0)
  • 2020-12-10 00:46

    A KeyedCollection allows mutable keys and ways to manage change in key. Dictionary does not allow changes to key. Secondly, if you have a collection which needs lookup, the logic to extract key from entity remains in one place - whereas maintaining dictionary will need to put key extraction logic at each place where items are added/removed from dictionary.

    0 讨论(0)
  • 2020-12-10 00:46

    A KeyedCollection should be used when the key is on the item itself.

    By default, KeyedCollection is a Collection<TItem> wrapper around a dictionary. When you use small collections and/or you prefer retrieving items directly, the KeyedCollection provides a constructor that takes a dictionaryCreationThreshold parameter, that indicates at what collection count to switch to Dictionary.
    Another aspect in KeyedCollection is that you can choose to switch the key property (as long as their types match). This can be good for double keyed items etc. Performancewise, I don't think wrapping a dictionary has much overhead except if you generate a bunch of KeyedCollection instances, or if you use really large collections (there are some internal null checks to determine if there is a dictionary).
    One thing I'd hope to see in KeyedCollection is unabstracting it, but you can make a generic concrete type just as easy.

    0 讨论(0)
  • 2020-12-10 00:50

    By default a KeyedCollection creates a Dictionary under the covers.
    If the Key also has meaning as part of the Value and also defines uniqueness then that is the purpose of a KeyedCollection.

    If you want to modify the dictionary backing then use this ctor:

    protected KeyedCollection(
    IEqualityComparer<TKey> comparer,
    int dictionaryCreationThreshold)
    
    0 讨论(0)
  • 2020-12-10 01:00

    None of the previous comments address the most important difference between the two: KeyedCollection keeps your items in the order in which they are added (the first item added is at index 0 and the last added is at the last index). Dictionary does not (or at least it is never guaranteed to do so).

    This extra benefit of KeyedCollection does have a small performance cost. Under the covers, you pay the cost of maintaining both a Dictionary and a List.

    0 讨论(0)
  • 2020-12-10 01:05

    You can't use KeyedCollection because it's abstract: http://msdn.microsoft.com/en-us/library/ms132438.aspx. This means you can't create an object of it.

    0 讨论(0)
提交回复
热议问题