Home
Map
null Array ExampleUse null arrays and empty arrays. Test for a null array before accessing an element.
C#
This page was last reviewed on Sep 12, 2023.
Null array. A C# array can be null. As a field, an array is by default initialized to null. When using local variable arrays, we must specify this explicitly.
Shows an int array
Null fields. The C# language initializes array reference elements to null when created with the new keyword. Arrays that are fields are automatically set to null.
null
Nullable
new
This example shows that static arrays (such as int[] fields on a type) are by default initialized to null. This occurs automatically, with no special code.
int Array
Here The example demonstrates the difference between an empty array of zero elements and a null array reference.
Tip Local variables in .NET are stored in a separate part of the metadata. They do not implicitly initialize to null.
Tip 2 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. It writes to the screen with Console.WriteLine.
Console.WriteLine
Shows an int array
using System; class Program { // Empty array. static int[] _arrayField1 = new int[0]; // Null. static int[] _arrayField2; 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(_arrayField2 == null); // Default expression for arrays evaluates to null. Console.WriteLine(default(int[]) == null); } }
False True True True
Default value. Array elements are separate in memory from the array reference. The elements are initialized to null if the type is a reference type. Value types are initialized to 0.
Tip It is never worthwhile to loop through an array you allocate and assign all its elements to null.
Detail The program allocates a string array of 3 elements. These elements are initialized to null. We can test them against null.
Warning Using an instance member on one of the elements will result in a NullReferenceException.
Array
NullReferenceException
using System; // 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);
True True True
Notes, zero bits. In a new array, each element has its memory location set to all zero bits. Multiple zero bits all mean the same thing—they 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—we never need to loop over and set elements to null in a newly-created array.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 12, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.