Are You incrementing null???
int? a = null;
a++;
This statement simply means null++
i.e. null+1.
As per this document, A nullable type can represent the correct range of values for its underlying value type, plus an additional null value.A Nullable, pronounced "Nullable of Int32," can be assigned any value from -2147483648 to 2147483647, or it can be assigned the null value
Here you are incrementing null, then also it will become null value not 0 or any other integer.
Why it prints blank instead of error??
when you print a nullable type with null value it prints blank instead of error because you are printing a variable i.e. value of a memory location. which maybe null or any integer.
But when you try to print null using Console.WriteLine(null)
, as null is not a variable, so it doesn't refer to any memory location. And hence it gives error "NullReferenceException"
.
Then how can you print any integer using Console.WriteLine(2);
??
In this case, 2 will gets in memory at temporary location, and the pointer points to that memory location to print.