使用GetHash code获取枚举int值 [英] Using GetHashCode for getting Enum int value

查看:157
本文介绍了使用GetHash code获取枚举int值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个枚举

public enum INFLOW_SEARCH_ON
{
  ON_ENTITY_HANDLE = 0,         
  ON_LABEL = 1,                 
  ON_NODE_HANDLE = 2            
} // enum INFLOW_SEARCH_ON

我要使用此枚举在网格中列seraching

I have to use this enum for seraching in a grid columns

要获得我使用的列索引

  MyEnumVariable.GetHashCode() 

我应该使用哪种工程确定,或

Which works ok, or should I use

  (short)MyEnumVariable

我在使用GetHash code有点糊涂了()。有没有使用任何问题。

I am bit confused over using GetHashCode(). Is there any problem using that.

推荐答案

使用 GetHash code()不正确。你应该转换为 INT 。使用它,你要做的就是要求猛禽(或林峰)的方式来吃饭你。

Using GetHashCode() is incorrect. You should cast to int. Using it the way you do is asking for raptors(or Raymond) to come and eat you.

这是 GetHash code()恰好返回枚举的整数值是一个实现细节,并在.NET的未来版本可能会改变。

That GetHashCode() happens to return the integer value of the enum is an implementation detail and may change in future versions of .net.

GetHash code()保证如果两个值等于的哈希codeS是平等了。另一种方式圆不能保证。

GetHashCode() guarantees that if two values are equal their hash codes are equal too. The other way round is not guaranteed.

我的经验法则是,如果 GetHash code 是返回一个恒定值你的程序应该仍正常工作(但可能是的慢),因为一个恒定 GetHash code 平凡履行合同,但有坏分配属性。

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 constant GetHashCode trivially fulfills the contract, but has bad distribution properties.

这篇关于使用GetHash code获取枚举int值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆