All object references in .NET are basically 32-bit or 64-bit pointers.
I am curious about how null
is represented in memory. Is it actually 32 or 64 zeroes? Is it guaranteed to be so, according to specification?
I couldn't find the answer on MSDN or C# language specification.
There is also a similar question for C language, but what about C#?
A null pointer constant is an integer constant expression that evaluates to zero. For example, a null pointer constant can be 0, 0L , or such an expression that can be cast to type (void *)0 . You can specify any of the following values for a null pointer constant: 0.
The null pointer constant is always 0. The NULL macro may be defined by the implementation as a naked 0 , or a cast expression like (void *) 0 , or some other zero-valued integer expression (hence the "implementation defined" language in the standard). The null pointer value may be something other than 0.
Master C and Embedded C Programming- Learn as you goA null pointer is a pointer which points nothing. Some uses of the null pointer are: a) To initialize a pointer variable when that pointer variable isn't assigned any valid memory address yet.
int * pInt = NULL; To check for a null pointer before accessing any pointer variable. By doing so, we can perform error handling in pointer related code e.g. dereference pointer variable only if it's not NULL.
I couldn't find the answer on MSDN or C# language specification.
Bacause it does not matter. As you never can work with the pointer - this is an implementation detail.
You can check the CLR sources at https://github.com/dotnet/coreclr
THAT SAID - I think it is all 0 because when laoding that into a register, there should be (at least it was wehen i learned assembler) a special flag that the value is null, so the check is super fast.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With