Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nothing != null - or does it?

Tags:

c#

.net

vb.net

clr

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?

like image 370
Gavin Osborn Avatar asked Jul 09 '10 12:07

Gavin Osborn


People also ask

Is null the same as nothing?

Null is a specific subtype of a Variant. It has no existence outside of the Variant type, and is created to allow a Variant to model a database null value. Nothing is a value of an Object variable. It essentially is identical to a null pointer, i.e. there is no object.

Is VB nothing same as null?

When checking whether a reference (or nullable value type) variable is null , do not use = Nothing or <> Nothing . Always use Is Nothing or IsNot Nothing . For strings in Visual Basic, the empty string equals Nothing .

What is null equals null nothing?

NULL basically means no value, or unknown,NULL value could be either empty, meaningless, or not even initialized.

Does null 0 in Java?

Below are some important points about null in java that every Java programmer should know: 1. null is Case sensitive: null is literal in Java and because keywords are case-sensitive in java, we can't write NULL or 0 as in C language.


1 Answers

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

like image 125
Dan Puzey Avatar answered Oct 03 '22 14:10

Dan Puzey