简体   繁体   English

什么都没有!= null - 或者是吗?

[英]Nothing != null - or does it?

Recently in a previous project I came across a peculiar difference between VB.NET and C#. 最近在之前的一个项目中,我发现了VB.NET和C#之间的一个特殊区别。

Consider the following C# expression which: 考虑以下C#表达式:

null <= 2

This expression evaluates to False which is what I would expect. 此表达式的计算结果为False ,这正是我所期望的。 Then the corresponding VB.NET expression: 然后是相应的VB.NET表达式:

Nothing <= 2

I was surprised to learn that this expression actually evaluates to True 我很惊讶地发现这个表达式实际上是评估为True

It seems like a fairly fundamental design decision between the two languages and it certainly caught me out. 这似乎是两种语言之间相当基本的设计决定,它确实让我感到震惊。

Is anyone able to tell me why? 有人能告诉我为什么吗? Are null and Nothing one and the same? 是null和Nothing是一样的吗? If so, why do they behave differently? 如果是这样,他们为什么表现不同?

Nothing in VB evaluates to the default value for a given type. VB中的Nothing都不会计算为给定类型的默认值。 (See this link for details.) (有关详细信息,请参阅此链接 。)

For an integer comparison (which the compiler will assume from the right hand operand), Nothing will thus be 0 . 对于整数比较(编译器将从右手操作数假设),因此Nothing将为0 0 <= 2 is true for more obvious reasons :-) 出于更明显的原因, 0 <= 2是正确的:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM