有myNullableLong.HasValue和myNullableLong任何区别!= NULL? [英] Is there any difference between myNullableLong.HasValue and myNullableLong != null?

查看:230
本文介绍了有myNullableLong.HasValue和myNullableLong任何区别!= NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我有一个可为空长,例如,是否有

When I have a nullable long, for example, is there any difference between

myNullableLong.HasValue 



and

myNullableLong != null

...或只是语法糖?

... or is it just 'syntactic sugar'?

推荐答案

这只是语法糖。他们的行为完全一样的方式 - 在法律上无效实际测试被编译成一个调用的HasValue 反正

It's just syntactic sugar. They will behave exactly the same way - the nullity test actually gets compiled into a call to HasValue anyway.

样品。

public class Test
{
    static void Main()
    {
        int? x = 0;
        bool y = x.HasValue;
        bool z = x != null;
    }
}



IL:

IL:

.method private hidebysig static void  Main() cil managed
{
  .entrypoint
  // Code size       25 (0x19)
  .maxstack  2
  .locals init (valuetype [mscorlib]System.Nullable`1<int32> V_0)
  IL_0000:  ldloca.s   V_0
  IL_0002:  ldc.i4.0
  IL_0003:  call       instance void valuetype [mscorlib]System.Nullable`1<int32>::.ctor(!0)
  IL_0008:  ldloca.s   V_0
  IL_000a:  call       instance bool valuetype [mscorlib]System.Nullable`1<int32>::get_HasValue()
  IL_000f:  pop
  IL_0010:  ldloca.s   V_0
  IL_0012:  call       instance bool valuetype [mscorlib]System.Nullable`1<int32>::get_HasValue()
  IL_0017:  pop
  IL_0018:  ret
} // end of method Test::Main

这篇关于有myNullableLong.HasValue和myNullableLong任何区别!= NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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