TheDeveloperBlog.com


C# Null Array Use

Null array. An array can be null. An array is by default initialized to null. This must be specified explicitly in local variables. The C# language also initializes array reference elements to null when created with the new keyword.

NullNew

Example. First, this example shows that static arrays (such as int[] fields on a type) are by default initialized to null—even if you do not specify this. The same principle holds true if you are using instance objects on a type.

Int Array

Here: The example demonstrates the difference between an empty array of zero elements and a null array reference.

And: Finally, it reveals the result of the default expression for an array type.

Default
C# program that uses null array references

using System;

class Program
{
    static int[] _arrayField1 = new int[0]; // Empty array
    static int[] _arrayField2; // Null

    static void Main()
    {
	//
	// Shows an empty array is not a null array.
	//
	int[] array1 = new int[0];
	Console.WriteLine(array1 == null);

	//
	// Shows how to initialize a null array.
	//
	int[] array2 = null;
	Console.WriteLine(array2 == null);

	//
	// Static and instance field arrays are automatically null.
	//
	Console.WriteLine(_arrayField1 == null); // Empty array
	Console.WriteLine(_arrayField2 == null); // Null

	//
	// Default expression for arrays evaluates to null.
	//
	Console.WriteLine(default(int[]) == null);
    }
}

Output

False
True
False
True
True

Local variables in the .NET Framework are stored in a separate part of the metadata. They do not implicitly initialize to null. Fields that are of a reference type such as an array type like int[] are implicitly assigned to null.

And: The example finally reports that the default(int[]) expression is equal to null.


Default value. Array elements are separate from the array reference variable itself and stored separately in memory. The array elements are initialized to null also if the type is a reference type. Value types are initialized to all zero bits.

Tip: It is never worthwhile to loop through an array you allocate and assign all its elements to null. This occurs implicitly in the CLR.

C# program that tests default values

using System;

class Program
{
    static void Main()
    {
	//
	// Value for all reference elements in new array is null.
	//
	string[] array = new string[3];
	Console.WriteLine(array[0] == null);
	Console.WriteLine(array[1] == null);
	Console.WriteLine(array[2] == null);
    }
}

Output

True
True
True

The program allocates a string array of three elements. These elements are initialized to null in the runtime and you can test them against null. Using an instance member on one of the elements will result in a NullReferenceException.

String ArrayNullReferenceException

In a new value array, each element has its memory location set to all zero bits. A 64-bit integer has 64 zero bits. A 4-bit structure (such as the byte type) has 4 zero bits. These values are represented with the decimal value 0.

Info: The memory is never uninitialized or garbage as you would encounter in C or C++. This helps promote program reliability.


Summary. We tested array references for the null value. Array elements are initialized to null at creation. Static and instance array fields are initialized to null implicitly. We used the default() expression to determine a type's default value.

And: We showed how every element in an array is initialized to zero bits which are represented with null or 0 in different types.

Array Property, Return Empty Array