After declaring and instantiating an array, all of its elements take on default values. The default value for all numeric types is 0 and for all reference types is null. For enum types, the default value will be the element that has a value of 0–typically the first item listed in the enum.
1
2
3
4
5
6
7
8
| float[] nums = new float[4];float f1 = nums[0]; // 0.0Cat[] pack = new Cat[5];Cat c1 = pack[0]; // nullDays[] someDays = new Days[4];Days thatDay = someDays[2]; // Sunday (1st in list) |
The same rule applies for default values in a multi-dimensional array. Each element is 0-valued or null.
1
2
| int[,] nums = new int[2, 3];int aNum = nums[1, 2]; // 0 |
With jagged arrays, you typically only instantiate the first dimension. Elements of that dimension are null.
1
2
3
4
5
6
| int[][] jagged = new int[3][];object o = jagged[0]; // nullint n1 = jagged[0][0]; // Error: NullReferenceExceptionjagged[0] = new int[10];int n2 = jagged[0][2]; // 0 |

