这是preferred:可空&LT;&GT; .HasValue或可空&LT;&GT; != NULL? [英] Which is preferred: Nullable<>.HasValue or Nullable<> != null?
本文介绍了这是preferred:可空&LT;&GT; .HasValue或可空&LT;&GT; != NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我总是用(A)可空&LT;&GT; .HasValue
,因为我喜欢的语义。然而,最近我在做别人的现有code碱基他们用在(B)可空&LT;&GT; != NULL
完全相反。有没有理由使用了另一种呢,还是纯粹的preference?
(一)
诠释?一个;
如果(a.HasValue)
...
(二)
诠释? b:
如果(B!= NULL)
...
解决方案
编译器替换空比较一起的HasValue
的电话,所以没有真正的区别。只要做到取其更可读/更有意义,你和你的同事。
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屋!
查看全文