当您在 C# 中将枚举强制转换为 int 时,幕后会发生什么?
我正在寻找用 C# 实现模拟器。
我考虑的事情之一是创建与其字节值关联的所有操作码的枚举。但是,我想知道考虑到我需要多久访问该字节值才能执行诸如将其用作查找表中的索引等操作,这是否不是一个好主意。
当您将枚举转换为一个 int,会发生什么?这样的手术费用有多高?简单地将我的操作码按其名称定义为 const 字节会更谨慎吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它非常便宜 - 实际上,它实际上是一个无操作,假设枚举有一个底层类型
int
开始(这是默认的)。例如,下面是一个示例程序:Main
生成的代码(未优化):实际上,转换是为了编译器 - 内存中的数据已经是在适当的状态下,所以它只需要复制该值,就像将
int
复制到int
一样。如果枚举的基础类型不是
int
,则将枚举强制转换为int
与将基础类型强制转换为int
具有相同的效果int
。例如,如果基础类型为long
,您最终会得到类似于conv.i4
的内容,其方式与转换long
的方式相同> 通常为int
。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:And the generated code for
Main
(not optimized):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 anint
.If the underlying type of the enum isn't an
int
, then casting the enum toint
has the same effect as casting the underlying type toint
. For example, if the underlying type islong
, you'll end up with something likeconv.i4
in the same way that you would castinglong
toint
normally.这在一定程度上取决于枚举本身是否基于
int
;p如果是,什么也不会发生 - 枚举完全用其
int
表示/long
任何形式,直到您将它们装箱为止。从MyEnum : int
进行转换 <===>int
是一个无操作。您所熟悉的在方法重载解析等方面的大多数区别纯粹是针对编译器而存在的;在 IL 级别上没有任何区别。It depends a bit on whether the enum is itself based on
int
;pIf it is, nothing happens - enums are represented entirely in their
int
/long
whatever form until you box them. Casting fromMyEnum : 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.