慎用 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& ...
随机推荐
- discourse 基于ember.js+rails项目的安装部署
最近公司在讨论做一个ERP运维问答的论坛系统,看了很多开源系统,觉得discourse功能比较完善,灵活.可配置性非常好,部署方便,瀑布流的主题布局模式也很符合未来论坛的趋势,于是在 ucloud 上 ...
- C语言 文件操作1--二进制文件与文本文件
//写文件两种方式(文本文件和二进制文件) #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h&g ...
- 如何在高并发分布式系统中生成全局唯一Id(转)
http://www.cnblogs.com/heyuquan/p/global-guid-identity-maxId.html 又一个多月没冒泡了,其实最近学了些东西,但是没有安排时间整理成博文, ...
- 一道c语言运算符优先级问题
一道c语言运算符优先级问题 #include <iostream> using namespace std; int main() { char test[] = {"This ...
- 软件工程(QLGY2015)第二次作业点评(随机挑选20组点评)
相关博文目录: 第一次作业点评 第二次作业点评 第三次作业点评 说明:随机挑选20组点评,大家可以看看blog名字,github项目名字,看看那种是更好的,可以学习,每个小组都会反应出一些问题,希望能 ...
- python的闭包与装饰器
原文发表在我的博客主页,转载请注明出处 前言 如果把python当作脚本语言,每次就是写个几十行上百行来处理数据的话,装饰器也许不是很必要,但是如果要开发一个大型系统,装饰器是躲不开的,最开始体会ry ...
- grootJs的属性绑定指令
index6.html 绑定文本text gt-text="{属性名}" 绑定标签属性attr gt-attr="vm属性名称(标签属性,value表达式)" ...
- jdbc基础 (五) 连接池与数据源 DBCP以及C3P0的使用
一.连接池的概念和使用 在实际应用开发中,特别是在WEB应用系统中,如果JSP.Servlet或EJB使用JDBC直接访问数据库中的数据,每一次数据访问请求都必须经历建立数据库连接.打开数据库.存取数 ...
- 如何使用开源库,吐在VS2013发布之前,顺便介绍下VS2013的新特性"Bootstrap"
刚看到Visual Studio 2013 Preview - ASP.NET, MVC 5, Web API 2新功能搶先看 看了下VS2013带来的"新特性",直觉上看,除了引 ...
- 深入javascript
1.不定参数的使用 <!DOCTYPE html> <html> <head> <title>json</title> <script ...