简体   繁体   中英

Nothing != null - or does it?

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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