Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a general concrete implementation of a KeyedCollection?
    text
    copied!<p>The <a href="http://msdn.microsoft.com/en-us/library/ms132439.aspx" rel="noreferrer">System.Collections.ObjectModel.KeyedCollection</a> class is a very useful alternative to <a href="http://msdn.microsoft.com/en-us/library/xfhwa508.aspx" rel="noreferrer">System.Collections.Generic.Dictionary</a>, especially when the key data is part of the object being stored or you want to be able to enumerate the items in order. Unfortunately, the class is abstract, and I am unable to find a general concrete implementation in the core .NET framework.</p> <p>The <a href="http://rads.stackoverflow.com/amzn/click/0321545613" rel="noreferrer">Framework Design Guidlines</a> book indicates that a concrete implementation <em>should</em> be provided for abstract types (section 4.4 Abstract Class Design). Why would the framework designers leave out a general concrete implementation of such a useful class, especially when it could be provided by simply exposing a constructor that accepts and stores a <a href="http://msdn.microsoft.com/en-us/library/kt456a2y.aspx" rel="noreferrer">Converter</a> from the item to its key:</p> <pre><code>public class ConcreteKeyedCollection&lt;TKey, TItem&gt; : KeyedCollection&lt;TKey, TItem&gt; { private Converter&lt;TItem, TKey&gt; getKeyForItem = null; public ConcreteKeyedCollection(Converter&lt;TItem, TKey&gt; getKeyForItem) { if (getKeyForItem == null) { throw new ArgumentNullException("getKeyForItem"); } this.getKeyForItem = getKeyForItem; } protected override TKey GetKeyForItem(TItem item) { return this.getKeyForItem(item); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload