What is jagged array in C#.Net and when to prefer jagged arrays over multi-dimensional arrays?

Technology CommunityCategory: C#What is jagged array in C#.Net and when to prefer jagged arrays over multi-dimensional arrays?
VietMX Staff asked 3 years ago

jagged array (double[][]) is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. If you care about speed then you always prefer jagged arrays over multi-dimensional arrays. The .NET runtime support (jitter and CLR) makes a strong distinction between vectors and arrays. A jagged array is a vector of vectors, you get all of the runtime optimizations when you use them. Multi-dimensional arrays don’t. The speed difference is about a factor of two.

Only ever consider using multi-dimensional arrays if you favor the syntax convenience and have verified that array indexing is not on your program’s critical path.