慎用 Enum.GetHashCode()
公司里遗留下了相当多的 Enum.GetHashCode()来获取枚举值的代码
但是这会产生装箱行为的!!因为Enum是值类型,GetHashCode()是Object的方法,调用GetHashCode必定要装箱成Object类型才能调用
同理:Enum.ToString()也会装箱 用 Enum.GetName( typeof (Color), color);//推荐
namespace EnumBoxTest
{
class Program
{
enum Color
{
None=0,
Red=1,
Black=2
}
static void Main(string[] args)
{
int colorId = Color.Red.GetHashCode();
Console.WriteLine(colorId);
}
}
}
有IL代码有真相
在下面 IL_0002: box EnumBoxTest.Program/Color 代码中,可以清晰看到装箱行为
namespace EnumBoxTest
{
.class private auto ansi beforefieldinit EnumBoxTest.Program
extends [mscorlib]System.Object
{
// 嵌套类型
.class nested private auto ansi sealed Color
extends [mscorlib]System.Enum
{
// 成员
.field public specialname rtspecialname int32 value__
.field public static literal valuetype EnumBoxTest.Program/Color None = int32(0)
.field public static literal valuetype EnumBoxTest.Program/Color Red = int32(1)
.field public static literal valuetype EnumBoxTest.Program/Color Black = int32(2)
} // 类 Color 结束
// 方法
.method private hidebysig static
void Main (
string[] args
) cil managed
{
// 方法起始 RVA 地址 0x2050
// 方法起始地址(相对于文件绝对值:0x0250)
// 代码长度 21 (0x15)
.maxstack 1
.entrypoint
.locals init (
[0] int32 colorId
)
// 文档:\EnumBoxTest\Program.cs, 行 14,列 9
// 0x025C: 00
IL_0000: nop
// 文档:\EnumBoxTest\Program.cs, 行 15,列 13
// 0x025D: 17
IL_0001: ldc.i4.1
// 0x025E: 8C 03 00 00 02
IL_0002: box EnumBoxTest.Program/Color
// 0x0263: 6F 11 00 00 0A
IL_0007: callvirt instance int32 [mscorlib]System.Object::GetHashCode()
// 0x0268: 0A
IL_000c: stloc.0
// 文档: \EnumBoxTest\EnumBoxTest\Program.cs, 行 16,列 13
// 0x0269: 06
IL_000d: ldloc.0
// 0x026A: 28 12 00 00 0A
IL_000e: call void [mscorlib]System.Console::WriteLine(int32)
// 0x026F: 00
IL_0013: nop
// 文档:\EnumBoxTest\EnumBoxTest\Program.cs, 行 18,列 9
// 0x0270: 2A
IL_0014: ret
} // 方法 Program::Main 结束
.method public hidebysig specialname rtspecialname
instance void .ctor () cil managed
{
// 方法起始 RVA 地址 0x2071
// 方法起始地址(相对于文件绝对值:0x0271)
// 代码长度 7 (0x7)
.maxstack 8
// 0x0272: 02
IL_0000: ldarg.0
// 0x0273: 28 13 00 00 0A
IL_0001: call instance void [mscorlib]System.Object::.ctor()
// 0x0278: 2A
IL_0006: ret
} // 方法 Program::.ctor 结束
} // 类 EnumBoxTest.Program 结束
}
现在将代码换为
namespace EnumBoxTest
{
class Program
{
enum Color
{
None=0,
Red=1,
Black=2
}
static void Main(string[] args)
{
int colorId = (int)Color.Red;
Console.WriteLine(colorId);
}
}
}
IL代码为:
.namespace EnumBoxTest
{
.class private auto ansi beforefieldinit EnumBoxTest.Program
extends [mscorlib]System.Object
{
// 嵌套类型
.class nested private auto ansi sealed Color
extends [mscorlib]System.Enum
{
// 成员
.field public specialname rtspecialname int32 value__
.field public static literal valuetype EnumBoxTest.Program/Color None = int32(0)
.field public static literal valuetype EnumBoxTest.Program/Color Red = int32(1)
.field public static literal valuetype EnumBoxTest.Program/Color Black = int32(2)
} // 类 Color 结束
// 方法
.method private hidebysig static
void Main (
string[] args
) cil managed
{
// 方法起始 RVA 地址 0x2050
// 方法起始地址(相对于文件绝对值:0x0250)
// 代码长度 11 (0xb)
.maxstack 1
.entrypoint
.locals init (
[0] int32 colorId
)
// 文档: \EnumBoxTest\EnumBoxTest\Program.cs, 行 14,列 9
// 0x025C: 00
IL_0000: nop
// 文档:\EnumBoxTest\EnumBoxTest\Program.cs, 行 15,列 13
// 0x025D: 17
IL_0001: ldc.i4.1
// 0x025E: 0A
IL_0002: stloc.0
// 文档: \EnumBoxTest\EnumBoxTest\Program.cs, 行 16,列 13
// 0x025F: 06
IL_0003: ldloc.0
// 0x0260: 28 11 00 00 0A
IL_0004: call void [mscorlib]System.Console::WriteLine(int32)
// 0x0265: 00
IL_0009: nop
// 文档: \EnumBoxTest\EnumBoxTest\Program.cs, 行 18,列 9
// 0x0266: 2A
IL_000a: ret
} // 方法 Program::Main 结束
.method public hidebysig specialname rtspecialname
instance void .ctor () cil managed
{
// 方法起始 RVA 地址 0x2067
// 方法起始地址(相对于文件绝对值:0x0267)
// 代码长度 7 (0x7)
.maxstack 8
// 0x0268: 02
IL_0000: ldarg.0
// 0x0269: 28 12 00 00 0A
IL_0001: call instance void [mscorlib]System.Object::.ctor()
// 0x026E: 2A
IL_0006: ret
} // 方法 Program::.ctor 结束
} // 类 EnumBoxTest.Program 结束
}
性能测试如下:
enum MyEnum
{
Blank=0,
Write=1
}
class Program
{
static void Main(string[] args)
{
int v = 0;
Stopwatch sw=new Stopwatch();
sw.Reset();
sw.Start();
for (int i = 0; i < 1000*1000;i++)
{
v = MyEnum.Blank.GetHashCode();
}
sw.Stop();
Console.WriteLine("GetHashCode:{0}",sw.ElapsedMilliseconds);//耗时 81ms
sw.Reset();
sw.Start();
for (int i = 0; i < 1000 * 1000; i++)
{
v = (int)MyEnum.Blank ;
}
sw.Stop();
Console.WriteLine("(int):{0}", sw.ElapsedMilliseconds);//耗时 2ms
Console.Read();
}
}
慎用 Enum.GetHashCode()的更多相关文章
- Enum.GetHashCode()的问题
先说一下,正常如果代码可以定义成枚举,我是比较倾向于定义成枚举的,类似这样: public enum Gender { /// <summary> /// 男 /// </summa ...
- unity踩坑2020-01-21
这几天一直在测试一个类似于传奇的2d界面游戏,目前做的测试为: 人物动作响应,主要是8方向的判断和资源文件精灵的刷新. 学到的知识点: 1,Enum.GetHashCode() 可以得到这个枚举的索引 ...
- enum操作--获取枚举里的最大值
一个应用系统,如果程序里没有任何enum的使用,我认为它的可读性是有待商榷的. 求枚举里的最大/最小枚举值, 其实是对Array进行操作: enum EnumTest { ddd = , eee } ...
- C# EnumHelper Enum的值,Description,ToString()的相互转换
首先定义枚举类型,如下: /// <summary> /// 板块 /// </summary> public enum Plate { [Descriptio ...
- MVC图片上传详解 IIS (安装SSL证书后) 实现 HTTP 自动跳转到 HTTPS C#中Enum用法小结 表达式目录树 “村长”教你测试用例 引用provinces.js的三级联动
MVC图片上传详解 MVC图片上传--控制器方法 新建一个控制器命名为File,定义一个Img方法 [HttpPost]public ActionResult Img(HttpPostedFile ...
- C#中正确使用enum做Key的姿势
C#中自定义enum,然后将其作为Dictionary的Key,通常的做法如下: using System; using System.Text; using System.Collections.G ...
- Enum枚举类使用集合
1.使用扩展方法使用枚举值对于的Description属性值 public static class EnumExtenstion { public static string GetDescript ...
- C# 中 枚举Enum 一些转换的方法整理
工作中 经常遇到枚举 的一些转换 特别是获取枚举备注等 特地整理下 方法以后使用 public void TestMethod1() { TestEnumOne colorEnum = TestE ...
- Swift enum(枚举)使用范例
//: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...
随机推荐
- ABP入门系列——使用ABP集成的邮件系统发送邮件
ABP中对邮件的封装主要集成在Abp.Net.Mail和Abp.Net.Mail.Smtp命名空间下,相应源码在此. #一.Abp集成的邮件模块是如何实现的 分析可以看出主要由以下几个核心类组成: E ...
- Protocol Buffer多态
java中有多态的概念,protobuf本身没有多态的概念,但是它有一个扩展的概念. 以聊天消息为例,先看下面这个类图,基类是ChatMessage,子类TextMessage和ImageMessag ...
- Oracle11G安装之后
本人对oracle还处于摸索阶段,今天安装了一下Oracle11G, 安装之后,后台管理端的登录地址:https://172.16.10.75:1158/em 1.使用之前设置的dba管理员密码账号登 ...
- Google proto buffer的安装/使用
protobuf安装/使用原本是要在官网上下载的:http://protobuf.googlecode.com/files/protobuf-2.5.0.tar.gz可惜已被墙,幸好有好心人提供了以下 ...
- VBS基础篇 - 过程(sub 与 Function)
VBS基础篇 - 过程(sub 与 Function) 在VBscript中,有两种procedure:Sub procedure与Function procedure Sub过程:是包含在 Sub ...
- [MetaHook] Quake FMOD function
QFMOD.h #ifndef QFMOD_H #define QFMOD_H #include "fmod.h" extern FMOD_RESULT (F_API *qFMOD ...
- .NET中常用的几种解析JSON方法
一.基本概念 json是什么? JSON:JavaScript 对象表示法(JavaScript Object Notation). JSON 是一种轻量级的数据交换格式,是存储和交换文本信息的语法. ...
- Promise 学习笔记 - 时间支配者
本文同步自我的个人博客:http://www.52cik.com/2015/11/08/promise.html JavaScript 的 promises 事实标准称为 Promises/A+.ES ...
- unity3d 扩展NGUI —— 限制UI点击响应间隔
当某个按钮按下后给服务器发送某条消息 如果玩家短时间内疯狂点击按钮很多次,这将会给服务器发送很多条无用数据 不但增加了服务器的压力,发送数据还浪费流量,甚至可能引发一些莫名其妙的bug 所以,限制UI ...
- servlet设置缓存时间以及文件的下载
缓存时间的设置: public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletE ...