What happens when i set a variable to nothing in VB.NET? Is it true that nothing equals to default, or am i missing something here?
It equals default on ValueTypes or Structs, and is equal to null on Object types.
If it's a value type (like Integer, Double, etc.) setting the variable to Nothing will set it to the default value.
If it's a reference type, it will really be set to Nothing (null value).
Assigning Nothing to a variable sets it to the default value for its declared type.
If the variable is of a reference type, a value of Nothing means that the variable is not associated with any object. The variable has a null value.
Assuming VB.NET is largely similar to C#, null
, which is called Nothing
in VB.NET means that a reference doesn't point to anything. All types have default values when they are declared but not assigned: for example, the default value for int
s is 0
. The default value for reference types is the null value Nothing
. So an unassigned variable of a reference type will have the value Nothing
(null).
There's good blog Article on Null vs Nothing by Eric Lippert
Nothing does not equal default in all cases.