Which has better performance, Enum or Int in a switch case?

Which code snippet is better to use when considering the performance for the switch case with enum and int as the case parameter:

A.

switch ((ToolbarButton)BtnId)
{
    case ToolbarButton.SHOWPROPERTYDIALOG:
         OnShowProperties();
         break;
    case ToolbarButton.MOVETOFIRST:
         OnFirstMessage();
         break;
    case ToolbarButton.MOVETOLAST:
         OnLastMessage();
         break;
}

B.

switch (BtnId)
{
     case (int)ToolbarButton.SHOWPROPERTYDIALOG:
          OnShowProperties();
          break;
     case (int)ToolbarButton.MOVETOFIRST:
          OnFirstMessage();
          break;
     case (int)ToolbarButton.MOVETOLAST:
          OnLastMessage();
          break;
}

Answers


Once compiled, Enums ARE Ints.

There is No difference what-so-ever in the MSIL.


When compiling, the JIT replaces Enums with Int32 type. This is known as inline replacement and hence there is no performance hit. I would prefer using Enums as they increase readability and can be back-tracked (Find Reference) as well.


Need Your Help

Does Seaside scale?

scalability smalltalk seaside

Seaside is known as "the heretical web framework". One of the points that make it heretical is that it has much shared state. That however is something which, in my current understanding, hinders e...