title: Dictionary lookup throws "Index was outside the bounds of the array"
body: <p>I received an error report which seems to come from the following code:</p>
  
  <pre><code>public class AnimationChannelCollection : ReadOnlyCollection&lt;BoneKeyFrameCollection&gt;
  {
          private Dictionary&lt;string, BoneKeyFrameCollection&gt; dict =
              new Dictionary&lt;string, BoneKeyFrameCollection&gt;();
  
          private ReadOnlyCollection&lt;string&gt; affectedBones;
  
         // This immutable data structure should not be created by the library user
          internal AnimationChannelCollection(IList&lt;BoneKeyFrameCollection&gt; channels)
              : base(channels)
          {
              // Find the affected bones
              List&lt;string&gt; affected = new List&lt;string&gt;();
              foreach (BoneKeyFrameCollection frames in channels)
              {
                  dict.Add(frames.BoneName, frames);
                  affected.Add(frames.BoneName);
              }
              affectedBones = new ReadOnlyCollection&lt;string&gt;(affected);
  
          }
  
          public BoneKeyFrameCollection this[string boneName]
          {           
              get { return dict[boneName]; }
          }
  }
  </code></pre>
  
  <hr>
  
  <p>This is the calling code that reads the dictionary:</p>
  
  <pre><code>public override Matrix GetCurrentBoneTransform(BonePose pose)
      {
          BoneKeyFrameCollection channel =  base.AnimationInfo.AnimationChannels[pose.Name];       
      }
  </code></pre>
  
  <hr>
  
  <p>This is the code that creates the dictionary, happens on startup:</p>
  
  <pre><code>// Reads in processed animation info written in the pipeline
  internal sealed class AnimationReader :   ContentTypeReader&lt;AnimationInfoCollection&gt;
  {
      /// &lt;summary&gt; 
      /// Reads in an XNB stream and converts it to a ModelInfo object
      /// &lt;/summary&gt;
      /// &lt;param name="input"&gt;The stream from which the data will be read&lt;/param&gt;
      /// &lt;param name="existingInstance"&gt;Not used&lt;/param&gt;
      /// &lt;returns&gt;The unserialized ModelAnimationCollection object&lt;/returns&gt;
      protected override AnimationInfoCollection Read(ContentReader input, AnimationInfoCollection existingInstance)
      {
          AnimationInfoCollection dict = new AnimationInfoCollection();
          int numAnimations = input.ReadInt32();
  
          /* abbreviated */
  
          AnimationInfo anim = new AnimationInfo(animationName, new AnimationChannelCollection(animList));
  
      }
  }
  </code></pre>
  
  <p>The error is:</p>
  
  <blockquote>
    <p>Index was outside the bounds of the array.</p>
    
    <p>Line: 0</p>
    
    <p>at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)</p>
    
    <p>at System.Collections.Generic.Dictionary`2.get_Item(TKey key)</p>
    
    <p>at
    Xclna.Xna.Animation.InterpolationController.GetCurrentBoneTransform(BonePose
    pose)</p>
  </blockquote>
  
  <p>I would have expected a KeyNotFoundException with the wrong key, but instead I get "Index was outside the bounds of the array".
  I don't understand how I could get that exception from the above code?</p>
  
  <p>The code is singlethreaded by the way.</p>