C# Null List

Null keyword

A List can be null. This is not the same as it being empty and having zero elements. The null literal in the C# language is a special-cased zero value for reference types. It is used quite often with the List type.

Example

Note

First here we see an example program that declares three List references and checks them against the null literal in the C# language. The program shows clearly the difference between a List that is a field, such as a static List, and a List that is a local variable. It also show the difference between an empty List object and a null List reference.

Null Tips

This C# example program shows how to handle null List references.

Program that uses null Lists [C#]

using System;
using System.Collections.Generic;

class Program
{
    //
    // Field type List.
    //
    static List<int> _listField;

    static void Main()
    {
	//
	// Shows an empty List, not a null List.
	//
	List<string> listA = new List<string>();
	Console.WriteLine(listA == null);
	//
	// A null List reference.
	//
	List<string> listB = null;
	Console.WriteLine(listB == null);
	//
	// Calling an instance method on a null List causes a crash.
	//
	// listB.Add("cat");
	//
	// Static Lists and field Lists are automatically null.
	//
	Console.WriteLine(_listField == null);
	Console.WriteLine(default(List<bool>) == null);
    }
}

Output

False
True
True
True

Exception caused by null Lists

Unhandled Exception: System.NullReferenceException:
Object reference not set to an instance of an object.
   at Program.Main() in....
List type.

Local variable Lists. The program defines two new List variables. The first List variable listA is initialized to a new empty List object; this variable does not have the value of null. The second List variable listB is initialized to the null literal, and no memory is allocated on the managed heap in this assignment. If you call a method on this List, an exception will be thrown due to the null reference.

Note: Local variables are initialized in a different way than fields and class-level members.

Lists initialized to null at class-level. The program also defines the _listField member on the Program class type. This field is only located in one place in the entire program's memory. If you assign it in different places, the same reference will be changed. Fields such as the List field here and other reference types are automatically treated as null references when they are loaded.

Tip: You never need to initialize reference fields to null when you first encounter them.

Warning

NullReferenceException. The null reference exception occurs when you try to call an instance method on a reference type that has the value of null. The method table in the metadata is actually separate from the object instance data, but the callvirt instruction was used to cause the NullReferenceException to make code easier to debug.

NullReferenceException and Null Parameter

Custom null objects

Refactoring

The book Refactoring by Martin Fowler contains an interesting tutorial for replacing the null literal in a program with a "null object". This can simplify some complex logic and even reduce possible crashes in edge cases in your program. It makes sense to substitute a "dummy" object instance to stand in for cases where no data is available, such as when the customer information is absent on a purchase.

Summary

The C# programming language

We looked at null List references and how this adheres to the rules relating to reference types and null literals. Lists that are fields in a class, either static or instance, are initialized to null automatically; Lists that are local variables are not automatically initialized to null. You cannot call an instance method on a null List, and the default value expression returns null for the List constructed type.

List Examples Collections
.NET