C# 声明枚举时,是否应该强制类型为字节以减少 256 个实体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/648823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When declaring an enum, should you force the type to byte for under 256 entities?
提问by ghost
If you have an enum in your application and you only have a few items, should you force the underlying type to be the smallest possible type?
如果您的应用程序中有一个枚举并且您只有几个项目,您是否应该强制底层类型成为可能的最小类型?
enum smaller : byte
{
one,
two,
three
};
采纳答案by JaredPar
No. Don't prematurely optimize unlessyou've proved with a profiler that it's actually a problem.
不。不要过早地优化,除非你已经用分析器证明它确实是一个问题。
回答by jalf
What would be gained? You'd save a whopping 3 bytes of memory, at the cost of slightly slower execution and less intuitive or readable code. (Reading this, I have to wonder whether pyou actually had a reason for making it a byte, and what that reason might have been. Presumably you went out of your way to use a non-default type for a reason).
会得到什么?您将节省高达 3 字节的内存,代价是执行速度稍慢,代码不那么直观或可读。(读到这里,我不得不怀疑 pyou 是否真的有理由将它变成一个字节,以及那个原因可能是什么。大概你出于某种原因特意使用了非默认类型)。
If you plan to store millions of these things then yes, saving a few bytes on each may pay off. Otherwise, no.
如果您打算存储数百万个这样的东西,那么是的,在每个东西上节省几个字节可能会有回报。否则,没有。
It's the same reason you don't typically use byte or short instead of int.
这与您通常不使用 byte 或 short 而不是 int 的原因相同。
回答by Christian Klauser
You should not assign a certain integer type to enumerations but let the .NET environment figure out the best "size" for the enum. As JaredPar said, ifyou change the data type, you should definitelycheck whether it actually helps.
您不应为枚举分配某个整数类型,而应让 .NET 环境为枚举找出最佳“大小”。正如 JaredPar 所说,如果你改变了数据类型,你一定要检查它是否真的有帮助。
The thing is that 32-bit integers are "natural" on x86 processors because they can be easily align in an optimal fashion.
问题是 32 位整数在 x86 处理器上是“自然的”,因为它们可以以最佳方式轻松对齐。
回答by Brian R. Bondy
Relating to best practice:
最佳实践的相关资料:
When you don't have a particular reason for making the enum a type byte, you should leave it as the default.
当您没有将枚举设为类型字节的特定原因时,您应该将其保留为默认值。
Any time you use an enum in a switch statement you should have a "default" clause for an invalid enum value. So it doesn't matter if you are checking for 256-NumRealEnumValues or 2^32-NumRealEnumValues. Both will have a default clause that handles all invalid cases.
任何时候在 switch 语句中使用枚举时,都应该有一个无效枚举值的“默认”子句。因此,检查 256-NumRealEnumValues 或 2^32-NumRealEnumValues 并不重要。两者都有一个处理所有无效情况的默认子句。
One reason for explicitly setting the type of the enum, is if you want your enum to correspond to another type in your program and you need to explicitly cast between them.
显式设置枚举类型的一个原因是,如果您希望枚举对应于程序中的另一种类型,并且需要在它们之间显式转换。
Changing the type to the smallest fit will not help you with versioning problems either. Unless you have exactly the max size of the enum filled out. By versioning problems I mean when you have a compiled dll using the enum, then you add a new enum value, some code may execute that was not meant to go in the "default" clause of a switch statement.
将类型更改为最小拟合也不会帮助您解决版本问题。除非您完全填写了枚举的最大大小。通过版本控制问题,我的意思是当您使用枚举编译了 dll,然后添加一个新的枚举值时,可能会执行一些不打算放入 switch 语句的“默认”子句中的代码。
Relating to efficiency:
效率的相关资料:
No there is no benefit in terms of efficiency to make it a byte.
不,使它成为一个字节在效率方面没有任何好处。
int is more efficient to use because the cpu on x86 has 32-bit registers. Copying into a register is done 32-bits at a time.
int 使用起来更高效,因为 x86 上的 cpu 有 32 位寄存器。一次复制 32 位到寄存器中。
When you use a smaller type, you have to zero out part of the register and copy into the rest of the register's lower order bits.
当您使用较小的类型时,您必须将寄存器的一部分清零并将其复制到寄存器的其余低位中。
回答by Juliano
The only reason to do this is if you are storing or transmitting this value using a defined protocol that demands the field to be of that size.
这样做的唯一原因是,如果您使用要求字段具有该大小的已定义协议来存储或传输此值。
回答by Sergey Koval
If you are mapping the model with enum to another model, or serializing it, or your enum is reflected to the database column – then I would suggest you to specify the type explicitly.
如果您将带有 enum 的模型映射到另一个模型,或者对其进行序列化,或者您的 enum 反映到数据库列中 – 那么我建议您明确指定类型。
Scenario:
You have a column in database: status_id
with type tinyint
. And you have enum in your code: enum Status { Well = 1, Bad = 2 }
. And you use this enum in some entity. Let's say you use entity frameworks core 2.0. If you try to read/write this entity from database you will get the error "Unable to cast object", unless you specify the byte
type explicitly.
场景:您在数据库中有一个列:status_id
类型为tinyint
。你的代码中有 enum: enum Status { Well = 1, Bad = 2 }
。你在某个实体中使用这个枚举。假设您使用实体框架核心 2.0。如果您尝试从数据库读取/写入此实体,您将收到错误“无法转换对象”,除非您明确指定byte
类型。