1

In c#, Elements of a Jagged Array are 'Value type' or 'Reference type'? As Jagged arrays are array of arrays, I guess it should be a reference type not a value type. Also bc reference type can have null value.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Divyansh
  • 15
  • 3

1 Answers1

1

The term jagged array means that you have an array of an array type:

int[][] arr;

arr here is an array whith elements of type int[]. So yes, this is a reference type.

And as with all reference types, if you don't initialize the int[] elements of the outer array, they are null:

int[][] arr = new int[50][];
Console.WriteLine(arr[0] == null ? "null" : ? "not null");

gives

null

So it is a common gotcha for beginners to try to assign something like arr[0][0] = 5 before they initialize arr[0] = new int[desiredlength];.

René Vogt
  • 43,056
  • 14
  • 77
  • 99