null reference value

A reference type variable can be assigned to null.

null means the reference type variable points to nothing.


using System;

class Rectangle
{
    public int Width = 5;
    public int Height = 5;
}
class Program
{
    static void Main(string[] args)
    {
        Rectangle r1 = null;

        Console.WriteLine(r1);
    }
}

A value type variable cannot be assigned to null.


using System;

class Program
{
    static void Main(string[] args)
    {
        int i = null;

        Console.WriteLine(i);
    }
}

Compiling the code above generates the following error message:


C:\g>csc Program.cs
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.

Program.cs(7,17): error CS0037: Cannot convert null to 'int' because it is a non-nullable value type
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.