简体   繁体   中英

What happens under the hood when you cast an enum to an int in C#?

I'm looking to implement an emulator in C#.

One of the things I considered was creating an enum of all the opcodes associated with their byte value. However, I wonder if this might not be a good idea considering how often I'm going to need to access that byte value to do things like use it as the index in a lookup table, etc, etc.

When you cast an enum to an int, what happens? How expensive of an operation is this? Would it be more prudent to simply define my opcodes as const bytes by their name?

It's very cheap - it's effectively a no-op, really, assuming the enum has an underlying type of int to start with (which is the default). For example, here's a sample program:

using System;

enum Foo { A, B, C };

class Test
{
    static void Main()
    {
        Foo x = Foo.B;
        int y = (int) x;
    }    
}

And the generated code for Main (not optimized):

.method private hidebysig static void  Main() cil managed
{
  .entrypoint
  // Code size       6 (0x6)
  .maxstack  1
  .locals init (valuetype Foo V_0,
           int32 V_1)
  IL_0000:  nop
  IL_0001:  ldc.i4.1
  IL_0002:  stloc.0
  IL_0003:  ldloc.0
  IL_0004:  stloc.1
  IL_0005:  ret
} // end of method Test::Main

Effectively the cast is for the sake of the compiler - the data in memory is already in an appropriate state, so it just needs to copy the value just like it would copying an int to an int .

If the underlying type of the enum isn't an int , then casting the enum to int has the same effect as casting the underlying type to int . For example, if the underlying type is long , you'll end up with something like conv.i4 in the same way that you would casting long to int normally.

It depends a bit on whether the enum is itself based on int ;p

If it is, nothing happens - enums are represented entirely in their int / long whatever form until you box them. Casting from MyEnum : int <===> int is a no-op. Most distinctions that you are familiar with in terms of method overload resolution etc exist purely for the compiler; at the IL level there is no difference whatsoever.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM