这是preferred:可空<> .HasValue或可空<> == NULL?

编程入门 行业动态 更新时间:2024-10-17 09:51:18
本文介绍了这是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?

本文发布于:2023-11-02 14:15:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552629.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:这是   或可   preferred   GT   LT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!