title: In a switch vs dictionary for a value of Func, which is faster and why?
body: <p>Suppose there is the following code:</p>
  
  <pre><code>private static int DoSwitch(string arg)
  {
      switch (arg)
      {
          case "a": return 0;
          case "b": return 1;
          case "c": return 2;
          case "d": return 3;
      }
      return -1;
  }
  
  private static Dictionary&lt;string, Func&lt;int&gt;&gt; dict = new Dictionary&lt;string, Func&lt;int&gt;&gt;
      {
          {"a", () =&gt; 0 },
          {"b", () =&gt; 1 },
          {"c", () =&gt; 2 },
          {"d", () =&gt; 3 },
      };
  
  private static int DoDictionary(string arg)
  {
      return dict[arg]();
  }
  </code></pre>
  
  <p>By iterating over both methods and comparing, I am getting that the dictionary is slightly faster, even when "a", "b", "c", "d" is expanded to include more keys. Why is this so?</p>
  
  <p>Does this have to do with cyclomatic complexity? Is is because the jitter compiles the return statements in the dictionary to native code only once? Is it because the lookup of the dictionary is O(1), which <a href="https://stackoverflow.com/questions/44905/c-sharp-switch-statement-limitations-why">may not be the case for a switch statement</a>? (These are just guesses)</p>