这是preferred:可空<> .HasValue或可空<> == NULL? [英] Which is preferred: Nullable<>.HasValue or Nullable<> == null?

查看:157
本文介绍了这是preferred:可空<> .HasValue或可空<> == NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是用(A)可空<> .HasValue ,因为我喜欢的语义。不过,最近我在做别人的现有code基地,他们用(B)可空<> == NULL 完全代替。还有一个原因是使用一个比其他,抑或是纯粹的preference?

(一)

 诠释?一个;
如果(a.HasValue)
    ...
 

(二)

 诠释? B:
如果(B!= NULL)
    ...
 

解决方案

编译器替换为空的比较通过调用 hasVa​​lue的,所以没有真正的区别。只要做到取其更可读/更有意义,你和你的同事。

I always used (a)Nullable<>.HasValue because I liked the semantics. However, recently I was working on someone else's existing code base where they used (b)Nullable<> == null exclusively instead. Is there a reason to use one over the other, or is it purely preference?

(a)

int? a;
if(a.HasValue)
    ...

(b)

int? b;
if(b != null)
    ...

解决方案

The compiler replaces null comparisons with a call to HasValue, so there is no real difference. Just do whichever is more readable/makes more sense to you and your colleagues.

这篇关于这是preferred:可空&LT;&GT; .HasValue或可空&LT;&GT; == NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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