初始化'变种'为null [英] Initializing a 'var' to null

查看:105
本文介绍了初始化'变种'为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有下列变量初始化之间运行时的性能有什么区别



  VAR X = null作为对象; 
变种X =(对象)空;
对象x = NULL;


解决方案

我相信 。因为在编译的IL没什么区别

  VAR X = null作为对象; 
VAR X1 =(对象)空;
X2对象= NULL;



被编译到

  IL_0001:ldnull 
IL_0002:// stloc.0点¯x
IL_0003:ldnull
IL_0004:// stloc.1 X1
IL_0005:ldnull
IL_0006:// stloc.2 X2

您可以看到所有的当地人都使用初始化为null ldnull 操作码而已,所以没有什么区别。


Is there any difference in runtime performance between the following variable initializations?

var    x = null as object;
var    x = (object)null;
object x = null;

解决方案

I believe no, since there is no difference in compiled IL.

var    x = null as object;
var    x1 = (object)null;
object x2 = null;

gets compiled to

IL_0001:  ldnull      
IL_0002:  stloc.0     // x
IL_0003:  ldnull      
IL_0004:  stloc.1     // x1
IL_0005:  ldnull      
IL_0006:  stloc.2     // x2

You can see all the locals are initialized to null using ldnull opcode only, so there is no difference.

这篇关于初始化'变种'为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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