Like... is it 0
like in C++? Or is it some "special" object? Or maybe something totally different?
-- EDIT --
I do know what it is, the question is rather - how it's done
Like... is it 0
like in C++? Or is it some "special" object? Or maybe something totally different?
-- EDIT --
I do know what it is, the question is rather - how it's done
Since Java and C# run on virtual machines, it does not matter what is used physically to represent null, and it is not necessarily the same across implementations.
What matters is the behaviour of null, as defined in the language specification (see Dan's and MRFerocius' answers for details). Basically, it is a special value that variables of reference type can hold, and which cannot be dereferenced.
BTW, as a reference point, the Java serialization spec use a single byte value 0x70 to represent a null reference.
its equivalent to
in c++. So basically, yes, it is zero.
EDIT: Since I apparently didn't word this answer anywhere near correctly...
What I meant is this:
As C# runs in a VM, what Michael Borgwardt said is correct, we don't know how its represented behind the scenes. However, if you take the following code:
and compile it in a console app, with "Allow unsafe code" enabled, you will see that in c#, null is indeed equal to (void*) 0.
This is null in C#
null is a "reference" to nothing, so it points to literally nothing.
Additional info: A C# reference type can point to null (i.e.: nothing), a value type like int cannot point to null, although a valuetype can be used in the Nullable generic reference type.
Since you're asking about implementation details, rather than semantics, the answer is specific to a given implementation.
There are three things in C# that "null" can be. A reference, a pointer, and a nullable type.
The implementation of C# on the CLR represents a null reference by zero bits. (Where the number of bits is the appropriate size to be a managed pointer on the particular version of the CLR that you're running.)
Unsurprisingly, a null pointer is represented the same way. You can demonstrate this in C# by making an unsafe block, making a null pointer to void, and then converting that to IntPtr, and then converting the IntPtr to int (or long, on 64 bit systems). Sure enough, you'll get zero.
A null nullable value type is also implemented by zeroes, though in a different way. When you say
int? j = null;
what we actually create is the moral equivalent of:
With appropriate accessor methods, and so on. When one of those things is assigned null, we just fill the whole thing with zero bits. The value is the integer zero, and the hasValue is filled with zeroes and therefore becomes false; a nullable type with the hasValue field set to false is considered to be null.
I do not know what the implementation details are on other implementations of C# / the CLI. I would be surprised if they were different; this is the obvious and sensible way to implement nulls. But if you have questions about a specific implementation detail, you'll have to ask someone who knows about the implementation you're interested in.
Java Language Specification section 4.1: