All about Arrays in C#—Comparing Rectangular and Jagged Arrays
|Visual C# Tutorials|
|© 2006 Christian Gross|
Comparing Rectangular and Jagged Arrays
The structure of rectangular and jagged arrays is significantly different. For example, Figure 14-14 shows the structure of a rectangular three-by-three array, and a jagged array of three one-dimensional arrays of length 3.
- Both arrays hold nine integers, but as you can see, their structures are quite different.
- The rectangular array has a single array object, while the jagged array has four array objects.
One-dimensional arrays have specific instructions in the CIL that allow them to be optimized for performance. Rectangular arrays do not have these instructions, and are not optimized to the same level. Because of this, it can sometimes be more efficient to use jagged arrays of one-dimensional arrays—which can be optimized—than rectangular arrays, which cannot.
On the other hand, the programming complexity can be less for a rectangular array because it can be treated as a single unit, rather than an array of arrays.