Nothing != null - or does it?

可紊 提交于 2019-12-08 14:56:25

问题


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

Consider the following C# expression which:

null <= 2

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

Nothing <= 2

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.

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


回答1:


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

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 :-)



来源:https://stackoverflow.com/questions/3212536/nothing-null-or-does-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!