C# Null Array

Null keyword

An array can be null. An array type such as int[] is by default initialized to null. This must be specified explicitly when used as a local variable. The C# language also initializes array reference elements to the null value when you create them with the new keyword.

Example

Note

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. The example demonstrates the difference between an empty array of zero elements and a null array reference. Finally, it reveals the result of the default expression for an array type.

Null Tips

This C# program uses null arrays and empty arrays. It demonstrates syntax.

Program that uses null array references [C#]

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 you can 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
Main method

Overview. In the Main method, an empty array of zero elements is allocated and it is not equal to the null literal. Next, a local variable array is initialized to null. Local variables in the .NET Framework are stored in a separate part of the metadata and as part of this 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. Finally, the example reports that the default(int[]) expression is equal to null.

Default value

Default operator

Next here we see that array elements are separate from the array reference variable itself and stored separately in memory. However, the array elements are initialized to null also if the type is a reference type. Value types are initialized to the value representation equivalent to all zero bits. It is never worthwhile to loop through an array you allocate and assign all its elements to null, as this occurs implicitly in the CLR.

Program that tests null array elements [C#]

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

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

NullReferenceException and Null ParameterNew keyword (constructor invocation)

Defaults for value types. When you use the new expression on an array of value elements, each of the value elements has its memory location blanked out to all zero bits. This means a 64-bit integer will have 64 zero bits, while a 4-bit structure such as the byte type will have four zero bits. These values are represented with the decimal value 0. The memory is never uninitialized or garbage as you would encounter in C or C++.

Summary

The C# programming language

We saw how you can test array references for the null value and also how array elements are initialized to null on array creation. We noted how static and instance array fields are initialized to null implicitly; saw how you can use the default() expression to determine a type's default value; and 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 Array Types
.NET