#120 – Array Declaration and Instantiation

Three are three steps in declaring and using arrays in C#–declaration, instantiation and (optionally) initialization.

You start by declaring the array.

 int[] numbers;

At this point, the array has been declared but not instantiated.  We’ve declared a variable that references an array of integers, but no array has been created yet and the value of the variable is still null.

The next step is to instantiate the array.  We create the array of integers, assign memory for it, and set the variable to reference the newly created array.

 numbers = new int[4];    //  Instantiate

At this point, the array exists, but its elements all have default values.  For integer values, this default is 0.

We could declare and instantiate the array at the same time:

 int[] numbers = new int[4];    //  Declare / Instantiate