CSharp - Reference Type Null Value


A reference can be assigned the literal null.

null value indicates that the reference points to no object.

null is like a key without teeth.

class Point {}
Point p = null;
Console.WriteLine (p == null);   

// The following line generates a runtime error (a NullReferenceException is thrown):
//Console.WriteLine (p.X);

A value type cannot have a null value:

struct Point {}
Point p = null;  // Compile-time error
int x = null;    // Compile-time error