A reference can be assigned the literal null, indicating that the reference points to no object: - CSharp Language Basics

CSharp examples for Language Basics:Data Type

Description

A reference can be assigned the literal null, indicating that the reference points to no object:

Demo Code

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);
   }
}

Result

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

Related Tutorials