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.
Asked
Active
Viewed 595 times
1
-
I blame C++ and Java for making this confusing >:D – hoodaticus Feb 09 '17 at 20:12
1 Answers
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
-
Okay gotcha! So, it is just a reference type not a value type. Thanks! – Divyansh Feb 09 '17 at 19:58