title: Dictionary<K,V> that implements IList<V>
body: <p>I find myself often faced with this problem: I have a dictionary where the key is some simple numeric ID and the value is an object. The ID is also contained in a specific property of that value object.</p>
  
  <p>Then, I want to be able to deserialize some (format-inflexible) XML that looks like:</p>
  
  <pre><code>&lt;listitem&gt;
      &lt;id&gt;20359&lt;/id&gt;
      &lt;someotherval&gt;foo&lt;/someotherval&gt;
  &lt;/listitem&gt;
  &lt;listitem&gt;
      ...
  </code></pre>
  
  <p>This requires me to deserialize with a <code>List&lt;V&gt;</code>, and it's inconvenient to have to manually convert that to a <code>Dictionary&lt;K,V&gt;</code>.</p>
  
  <p>The second issue is with binding. Binding lists require that the source implements <code>ICollection</code> (if I remember correctly), and again it's a pain to have to manually create a new <code>List&lt;V&gt;</code> and populate it from the <code>Dictionary&lt;K,V&gt;</code>.</p>
  
  <p>My current, fairly ugly but functional solution is to have the following classes:</p>
  
  <pre><code>public abstract class Keyed&lt;KeyType&gt;
  {
   public KeyType key { get; set; }
  }
  
  public class KeyedDictionary&lt;KeyType, ValueType&gt; :
   Dictionary&lt;KeyType, ValueType&gt;
   where ValueType : Keyed&lt;KeyType&gt;
  {
   // ...
  }
  
  public class KeyedList&lt;KeyType, ValueType&gt; :
   IList&lt;ValueType&gt;,
   System.Collections.IList
   where ValueType : Keyed&lt;KeyType&gt;
  {
   public readonly KeyedDictionary&lt;KeyType, ValueType&gt; dict =
    new KeyedDictionary&lt;KeyType, ValueType&gt;();
  
   // ...
  }
  </code></pre>
  
  <p>This works, but it's internally large and ugly. Are there any better ways?</p>
  
  <hr>
  
  <p>EDIT: Here is the solution I've settled on.</p>
  
  <pre><code>public interface IKeyed&lt;KeyType&gt;
  {
      KeyType Key { get; }
  }
  
  public class KeyedList&lt;KeyType, ValueType&gt; :
      KeyedCollection&lt;KeyType, ValueType&gt;
      where ValueType : IKeyed&lt;KeyType&gt;
  {
      protected override KeyType GetKeyForItem(ValueType item) { return item.Key; }
  }
  </code></pre>