什么都没有!= null-还是吗? [英] Nothing != null - or does it?

查看:85
本文介绍了什么都没有!= null-还是吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近在上一个项目中,我遇到了VB.NET和C#之间的特殊区别。

Recently in a previous project I came across a peculiar difference between VB.NET and C#.

考虑以下C#表达式:

null <= 2

此表达式的计算结果为 False ,这正是我所期望的。
然后对应的VB.NET表达式:

This expression evaluates to False which is what I would expect. Then the corresponding VB.NET expression:

Nothing <= 2

我很惊讶地发现该表达式的计算结果为 True

I was surprised to learn that this expression actually evaluates to True

这似乎是两种语言之间的一个相当基本的设计决定,并且肯定使我失望。

It seems like a fairly fundamental design decision between the two languages and it certainly caught me out.

有人能告诉我为什么吗?
是否为null,什么都没有?
如果是这样,为什么它们的行为会有所不同?

Is anyone able to tell me why? Are null and Nothing one and the same? If so, why do they behave differently?

推荐答案

没事计算为给定类型的默认值。 (有关详细信息,请参见此链接。)

Nothing in VB evaluates to the default value for a given type. (See this link for details.)

对于整数比较(编译器将从右侧操作数假定),因此 Nothing 将为 0 0< = 2 是正确的,原因更加明显:-)

For an integer comparison (which the compiler will assume from the right hand operand), Nothing will thus be 0. 0 <= 2 is true for more obvious reasons :-)

这篇关于什么都没有!= null-还是吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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