Dim intResult As Integer = Nothing

VB.NET compiles:

Dim intResult As Integer = Nothing 

C # not:

 int intResult = null; // cannot convert 

How does the result finally go to MSIL?

Moreover, the VB code is fine:

 If intResult > Nothing Then 

EDIT

OK, MS says :

Assigning nothing to a variable sets the default value for the declared type.

But he says nothing about the Nothing comparison .

+4
source share
2 answers

Nothing in VB.NET is actually equivalent to default(Type) in C #.

So int intResult = default(int); is the equivalent of C #.

According to the VB.NET Language Reference : "Assigning nothing to a variable assigns a default value to the declared type. Type contains variable elements, all of them have default values."


Edit: Regarding the comparison with Nothing : I assume that intResult > Nothing interpreted as intResult > 0 , because the intResult time intResult type is Integer , which defaults to 0. If the intResult type is not known at compile time (for example, this is in Integer box), I suspect this will not compile.

See the Community Content section at the bottom of the VB.NET Language Reference page for a similar example ( Nothing < 2 ).

+8
source

Ints are not null in C # simply. If you want, you can use System.Nullable<int> short for whinch is

int? result = null int? result = null .

0
source

All Articles