Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dim intResult As Integer = Nothing

Tags:

c#

.net

vb.net

VB.NET compiles:

Dim intResult As Integer = Nothing

C# does not:

int intResult = null; // cannot convert

How the result finally goes to MSIL?

More that than, VB code is OK:

If intResult > Nothing Then

EDIT

OK, MS says:

Assigning Nothing to a variable sets it to the default value for its declared type.

But it tells nothing about Nothing comparation.

like image 250
serhio Avatar asked Feb 26 '23 15:02

serhio


1 Answers

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

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

According to the VB.NET Language Reference: "Assigning Nothing to a variable sets it to the default value for its declared type. If that type contains variable members, they are all set to their default values."


Edit: Regarding the comparison to Nothing: I'm guessing intResult > Nothing is interpreted as intResult > 0, because the compile-time type of intResult is Integer, which has a default value of 0. If the type of intResult is not known at compile time (e.g., it is a boxed Integer), I suspect that this would not compile.

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

like image 112
Justin Avatar answered Mar 03 '23 13:03

Justin