All about Arrays in C#—Instantiating a One-Dimensional or Rectangular Array
|Visual C# Tutorials|
|© 2006 Christian Gross|
Instantiating a One-Dimensional or Rectangular Array
To instantiate an array, you use an array creation expression. An array creation expression con¬sists of the
new operator, followed by the base type, followed by a pair of square brackets. The length of each dimension is placed in a comma-separated list between the brackets.
The following are examples of one-dimensional array declarations:
arr2is a one-dimensional array of four
mcArris a one-dimensional array of four
- Their layouts in memory are shown in Figure 14-5.
int arr2 = new int; MyClass mcArr = new MyClass;
- no. elements = 4 (i.e. "")
- Array creation expression = "new MyClass"
The following is an example of a rectangular array. Array
arr3 is a three-dimensional array.
- The length of the array is 3 * 6 * 2 = 36.
- Its layout in memory is shown in Figure 14-5.
int[,,] arr3 = new int[3,6,2] ;
- Lengths of the dimensions = "3,6,2"
At the time of instantiation, each element is automatically initialized to the default initialization value for the type of the element.
Note Array creation expressions do not contain parentheses—even for reference type arrays.