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

编程入门 行业动态 更新时间:2024-10-17 11:30:12
本文介绍了这是preferred:可空<> .HasValue或可空<> != NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我总是用(A)可空<> .HasValue ,因为我喜欢的语义。然而,最近我在做别人的现有code碱基他们用在(B)可空<> != 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?

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

发布评论

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

>www.elefans.com

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