Does the default match?

What happens when I set a variable to nothing in VB.NET? Is it true that nothing matches the default, or am I not seeing something here?

+3
source share
5 answers

If it is a value type (e.g. Integer, Double, etc.) that sets the variable to Nothing, it sets the default value.

If it is a reference type, it will actually be set to Nothing (null).

In Microsoft Words :

Assigning nothing to variable sets is its default value for its declared type.

If the variable has a reference type, the value Nothing means that the variable is not associated with any object. The variable is null.

+8
source

It is equal by default for ValueTypes or Structs and equal to zero for object types.

+1
source

What really can bite you is that if you have such a construction in C #

int? result = (a != null ? ab : (int?)null); 

and you replace it with the next VB (which compiles)

 Dim result As Integer? = If(a IsNot Nothing, ab, Nothing) 

What will happen?

The answer is that it will be 0, not null.

+1
source

Assuming VB.NET is a lot like C #, null called Nothing in VB.NET means the link does not point to anything. All types have default values ​​if they are declared but not assigned: for example, the default value for int is 0 . The default value for reference types is Nothing . Thus, an unassigned variable of a reference type will have the value Nothing (null).

0
source

There's a nice Null vs Nothing blog article by Eric Lippert

In all cases, nothing matches the default.

0
source

All Articles