
Null points to no object. Here we see some information and facts about the null literal ("null") in the C# language. Null is represented by the value 0, but you cannot use 0 instead of null in your programs. Null causes lots of exceptions.
KeywordsThis C# example set explores the null literal. Null is not the same as zero.

This program shows how you can assign an object reference to the null literal. You can use null with any reference type; this includes strings, arrays, and more unique types such as StringBuilder or custom types. Also, this program shows how the intermediate language represents the null literal (with ldnull).
Program that assigns reference to null [C#]
using System;
class Program
{
static void Main()
{
object value = new object();
value = null;
Console.WriteLine(value);
}
}
Intermediate language of the program [IL]
.method private hidebysig static void Main() cil managed
{
.entrypoint
.maxstack 1
.locals init (
[0] object 'value')
L_0000: newobj instance void [mscorlib]System.Object::.ctor()
L_0005: stloc.0
L_0006: ldnull
L_0007: stloc.0
L_0008: ldloc.0
L_0009: call void [mscorlib]System.Console::WriteLine(object)
L_000e: ret
}In the C# language, null is not the same thing as the constant zero. The C# compiler makes sure the two values are not used in inappropriate contexts. In the IL language, null is also kept separate. However, null is equal to zero at the machine level according to page 285 of Expert .NET 2.0 IL Assembler.
Expert .NET 2.0 IL Assembler Review
What happens when you assign a reference to the null literal? There is no null object allocation; instead, the value zero is copied to the reference. In the C# language, all assignments are simple bitwise copies. These are extremely fast to execute.
These example programs show how you can assign an array reference to the null literal. When you then try to access a property or method on that null reference, you get a NullReferenceException. Afterwards, we show a program that checks the array against the null literal and avoids the exception.
Null Array Use NullReferenceException and Null ParameterProgram that assigns null to array [C#]
using System;
class Program
{
static void Main()
{
string[] array = { "a", "b", "c" };
array = null;
int value = array.Length;
Console.WriteLine(value);
}
}
Result
(Output was truncated.)
Unhandled Exception: System.NullReferenceException:
Object reference not set to an instance of an object.
at Program.Main() in C:\Users\...\Program.cs:line 9
Corrected program that assigns null to array [C#]
using System;
class Program
{
static void Main()
{
string[] array = { "a", "b", "c" };
array = null;
if (array != null)
{
int value = array.Length;
Console.WriteLine(value);
}
}
}
Result
(It doesn't crash.)
Because strings are a reference type, they too can be null. You will often want to protect your program against null strings. You can do this with string.IsNullOrEmpty; string.IsNullOrWhitespace; or just checking against the null literal.
string.IsNullOrEmpty Method string.IsNullOrWhiteSpace Method Null String UsageFields that are reference types are always initialized to null when the enclosing type is instantiated. Therefore, you never have to initialize fields to null in your class constructor.
Class Examples
Here we looked at some information pertaining to the null literal in the C# programming language. Null is an important concept to grasp in imperative languages. We also discovered some neat low-level machine details about null.
Method Tips