使用 GetHashCode 获取 Enum int 值
我有一个枚举
public enum INFLOW_SEARCH_ON
{
ON_ENTITY_HANDLE = 0,
ON_LABEL = 1,
ON_NODE_HANDLE = 2
} // enum INFLOW_SEARCH_ON
,我必须使用这个枚举在网格列中进行搜索。
为了获取我正在使用的列索引,
MyEnumVariable.GetHashCode()
哪个工作正常,或者我应该使用
(short)MyEnumVariable
我对使用 GetHashCode()
感到有点困惑。使用起来有什么问题吗?
I have an enum
public enum INFLOW_SEARCH_ON
{
ON_ENTITY_HANDLE = 0,
ON_LABEL = 1,
ON_NODE_HANDLE = 2
} // enum INFLOW_SEARCH_ON
I have to use this enum for searching in a grid column.
To get the column index I am using
MyEnumVariable.GetHashCode()
Which works ok, or should I use
(short)MyEnumVariable
I am a bit confused over using GetHashCode()
. Is there any problem using that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用
GetHashCode()
是不正确的。您应该转换为int
。你使用它的方式就是要求猛禽(或雷蒙德)来吃你。GetHashCode()
恰好返回枚举的整数值,这是一个实现细节,在 .net 的未来版本中可能会发生变化。GetHashCode()
保证如果两个值相等
,它们的哈希码也相等。反之则不能保证。我的经验法则是,如果
GetHashCode
返回一个常量值,您的程序应该仍然可以正常工作(但可能慢很多),因为常量GetHashCode code> 基本上履行了契约,但具有不良的分发属性。
Using
GetHashCode()
is incorrect. You should cast toint
. Using it the way you do is asking for raptors(or Raymond) to come and eat you.That
GetHashCode()
happens to return the integer value of the enum is an implementation detail and may change in future versions of .net.GetHashCode()
guarantees that if two values areequal
their hash codes are equal too. The other way round is not guaranteed.My rule of thumb is that if
GetHashCode
were to return a constant value your program should still work correctly (but potentially be much slower) since a constantGetHashCode
trivially fulfills the contract, but has bad distribution properties.当我进行分析时,Enum.GetHashCode 花费了很多周期。我能够通过尽可能使用 (int)Enum 来改进它。
When I did profiling, Enum.GetHashCode took a lot of cycles. I was able to improve it by using (int)Enum where possible.
其他人已经说过为什么转换为 int 更好。
不使用 GetHashCode 的另一个原因是 性能。它会导致装箱。 在我的快速测试中,
GetHashCode
大约慢了 50 倍(如果有影响的话)。Others have said why casting to int is better.
Another reason to not use
GetHashCode
is performance. It causes boxing. In my quick tests,GetHashCode
was about 50 times slower (if it matters).您应该使用
(int)MyEnumVariable
来获取文字值...您也可以使用其他方式进行转换,例如(INFLOW_SEARCH_ON)int
You should use
(int)MyEnumVariable
for getting the literal value... you can also convert the other way like(INFLOW_SEARCH_ON)int
您应该使用
(int) MyEnumVariable
。默认情况下,所有枚举都继承自int
。You should use
(int) MyEnumVariable
. All enums by default are inherited fromint
.我看到一些评论描述了使用 GetHashCode() 的缺点,原因有很多。
在某些情况下 (int)INFLOW_SEARCH_ON.ON_LABEL 不起作用。
从 Enum 获取 int 值的一个好方法是使用 GeTTypeCode() 方法。无论底层类型如何,此方法都有效。在本例中它是 int。
本例中的输出为“1”。
参考:从枚举中获取 int 值
I see some comments that describe the disadvantages of using GetHashCode() for many reasons.
There are some cases where (int)INFLOW_SEARCH_ON.ON_LABEL doesn't work.
A good way to get the int value from Enum is using GeTTypeCode() method. This method works regardlesss of the underlying type. In this case it is int.
Output in this case is '1'.
Reference: Get int value from enum
您还可以使用 Enum 静态类提供的选项方法。例如:
如果您想要做的只是获取枚举的值。您可以使用
Enum.GetName(...)
获取字符串ValueType,例如“ON_ENTITY_HANDLE”。否则,强制转换为 (int) 肯定会优于
GetHashCode()
。You also have the option methods provided by the Enum static class. For instance:
If all you are trying to do is get the value of the Enum. You can get the string ValueType, e.g. "ON_ENTITY_HANDLE" with
Enum.GetName(...)
.Otherwise, casting to (int) would definitely be preferred over
GetHashCode()
.