title: ExpandoObject vs. Dictionary from a performance point of view?
body: <p>A rather simple question really. I'm working on a project where I need to store and retrieve property values dynamically from a kind of context storage. The values will be written now and then and read multiple times. Speed of retrieval is the top priority here, and every nanosecond counts. </p>
  
  <p>Usually, I'd simply implement this with a Dictionary, but with C# 4 and the ExpandoObject I'm thinking that maybe there is a better way? Does anyone have any experience with it? I've seen in other posts that it is NOT implemented using a Dictionary, which makes me curious as to whether it is quicker or slower?</p>
  
  <p>Let me try to clarify with some pseudo code:</p>
  
  <pre><code>// In the main loop
  var context = new Context();
  context["MyKey"] = 123;
  context["MyOtherKey"] = "CODE";
  context["MyList"] = new List&lt;int&gt;() { 1, 12, 14 };
  
  foreach(var handler in handlers) {
      handler.DoStuff(context);
  }
  </code></pre>
  
  <p>-</p>
  
  <pre><code>// "Handlers"
  class MyFirstHandler {
       void DoStuff(Context context) {
            if (context["MyKey"] &gt; 100)
                 context["NewKey"] = "CODE2";
       }
  }
  
  class MySecondHandler {
       void DoStuff(Context context) {
            if (context["MyOtherKey"] == "CODE")
               context["MyList"].Add(25); // Remember, it's only Pseudo-code..
       }
  }
  </code></pre>
  
  <p>Well, hopefully you get what I'm trying to do..</p>
  
  <p>I'm also completely open to other suggestions here. I have been toying with the idea of making the Context class statically typed (i.e. actually having a <code>MyKey</code> property, a <code>MyOtherKey</code> property etc), and while it might be possible it would hinder productivity quite a lot for us. </p>