Replace Switch Statements with Dictionary and Func<>

added by csurfleet
3/9/2012 6:07:36 AM


Continuing with the general theme of Func<>, Action<> and generics here is a nice little trick to replace long but simple switch statements with a generic dictionary and a Func statement.


3/9/2012 9:20:23 AM
It's a more elegant solution, I wonder how it effects the MSIL generated by the compiler? Generally switch statements translate to a series of equality checks, or a dictionary lookup depending on the size of the statement.

3/9/2012 9:28:02 AM
Hmm, its not something I've looked at to be honest, could be a nice little excersise for the weekend!

3/12/2012 9:42:31 AM
I didn't get chance to do the MSIL stuff over the weekend but if you were thinking about speed issues then there are a couple of comments on the post which might be interesting to you.