tags:

views:

454

answers:

9

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

+1  A: 

This is null in C#

Oskar
A: 

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.

Webleeuw
A: 

its equivalent to

#define NULL (void*)0

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:

    unsafe static void Main(string[] args)
    {
        if (((void*)0) == null)
            Console.WriteLine("true");
        else
            Console.WriteLine("false");

        Console.ReadKey();
    }

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.

TJMonk15
-1: null in C# is not a valuetype. This is an oversimplification at best.
Brian
To be pedantic about C++: a compile-time constant integer equal to zero, evaluated in a pointer context, is a **null pointer constant**. That is, zero is how you write a null pointer constant in your code, but a null pointer at run time is **not** zero.
Victor Nicollet
argh
Actually a null pointer is 0. It refers to the memory address of 0x00000000:00000000, which in Windows, and Linux, is a null pointer. Read the question again. It seems to me that he understands what null means, he wants to know how its done. And its done in the same way as c++.
TJMonk15
Oh, just to clarify, I'm only speaking about C# here. Not familar enough with java to answer that part...
TJMonk15
@TJMonk: "Oh, just to clarify, I'm only speaking about C# here." -- That was C++ code. C# null does not necessarily have to point to a 0 address...
yodaj007
I think you had some crude Idea in mind .. Just couldn't accomplish in words .. Not to be rude +1 .. :-)
infant programmer
@yodaj007: I was refering to how C# defines null. I used C++ code because the OP refers to C++ in his question, and I assumed he would understand the C++ snippet.
TJMonk15
@Brian: I never said null was a value type.
TJMonk15
+1  A: 

It is a special reference ( to differentiate it from 0 ) which point to nothing.

OscarRyz
+7  A: 

Java Language Specification section 4.1:

There is also a special null type, the type of the expression null, which has no name. Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type. The null reference is the only possible value of an expression of null type. The null reference can always be cast to any reference type. In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type.

Dan Dyer
+9  A: 

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.

Michael Borgwardt
That is all true, but for my perverse "need to know" thing it matters to me what is used physically to represent null ;)
argh
As I said: that may be different for each JVM or .NET implementation, and as an implementation detail, it's not something widely published.
Michael Borgwardt
Doh, it would be odd to see the same thing... an since it's not widely published - I've came here so maybe someone knows...
argh
+2  A: 

From Microsoft MDSN:

The null keyword is a literal that represents a null reference, one that does not refer to any object. null is the default value of reference-type variables. Ordinary value types cannot be null. However, C# 2.0 introduced nullable value types. See Nullable Types (C# Programming Guide).

MRFerocius
This explains the null keyword, but not how the null reference is implemented.
Will Eddins
+8  A: 

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:

struct NullableInt 
{
    int value;
    bool hasValue;
}

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.

Eric Lippert
A: 

Null is literally nothing. Some languages like PHP will equate null to 0, false, empty string, etc. under certain circumstances. Java and C# are strongly typed languages. Therefore none of those "helpful" implicit conversions take place. So null is literally null or nothing. Null is only equal to null. No other comparison will return true.

Laran Evans