What is better performance, Enum or Int in the switch enclosure?

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

and.

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

AT.

switch (BtnId)
{
     case (int)ToolbarButton.SHOWPROPERTYDIALOG:
          OnShowProperties();
          break;
     case (int)ToolbarButton.MOVETOFIRST:
          OnFirstMessage();
          break;
     case (int)ToolbarButton.MOVETOLAST:
          OnLastMessage();
          break;
}
+5
source share
2 answers

After compiling Enums ARE Ints .

There is no difference in MSIL.

+23
source

At compilation, JIT replaces Enums with an Int32 type. This is known as a built-in replacement, and therefore there is no performance. I would prefer to use Enums, as they increase readability and can be tracked (Find Reference).

+3
source

All Articles