title: "virtual" switch statement in C#?
body: <p>I'm writing a class that reads a text file and creates some data structures based on it.  I have no control over the file format, but it is consistent and I have many examples.  Every now and then, I encounter a page header.  I pull out a string from the header, and that informs what I do with the rest of the page.  I use a switch statement like this:</p>
  
  <pre><code>private void ReadPage(headerKey)
  {
      switch (headerKey)
      {
          case "a header": DoThis(headerKey); return;
          case "another header": DoThat(headerKey, "A"); return;
          case "header 3": DoThat(headerKey, "B"); return;
          // 10-20 more cases
      }
  }
  </code></pre>
  
  <p>It works well, but now I have made this class abstract and created two subclasses.  There are a few cases in the switch statement that apply to both subclasses, but each has some unique cases.  I'm trying to code this in a way that still looks pretty.  I could use a dictionary with a virtual method that populates it, but the syntax for that is so ugly, and I have the overhead of creating a dictionary for each instance of the class.</p>
  
  <pre><code>private readonly Dictionary&lt;string, Action&lt;string&gt;&gt; headerKeyCases();
  
  private A()
  {
      headerKeyCases = new Dictionary&lt;string, Action&lt;string&gt;&gt;(GetVariableGridParsersByName);
  }
  
  protected virtual IEnumerable&lt;KeyValuePair&lt;string, Action&lt;string&gt;&gt;&gt; GetVariableGridParsersByName()
  {
      yield return new KeyValuePair&lt;string, Action&lt;string&gt;&gt;("a header", headerKey=&gt; DoThis(headerKey));
      yield return new KeyValuePair&lt;string, Action&lt;string&gt;&gt;("another header", headerKey=&gt; DoThat(headerKey, "A"));
      yield return new KeyValuePair&lt;string, Action&lt;string&gt;&gt;("header 3", headerKey=&gt; DoThat(headerKey, "B"));
      // 10-20 more cases
  }
  </code></pre>
  
  <p>Is there a cleaner way to do this, or am I just being too picky?</p>