CSharp examples for Language Basics:Data Type
A reference can be assigned the literal null, indicating that the reference points to no object:
using System;/*from w ww.jav a2s . com*/ public class Point { public int X, Y; } class Test { static void Main(){ Point p = null; Console.WriteLine (p == null); // True // The following line generates a runtime error // (a NullReferenceException is thrown): Console.WriteLine (p.X); } }
In contrast, a value type cannot ordinarily have a null value:
public struct Point { public int X, Y; } Point p = null; // Compile-time error int x = null; // Compile-time error