When creating a 3D array in C#, which is a better way to create it?
Array[a,b,c] or Array[a][b][c]?
Or is it just a matter of preference?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
These are two fundamentally different constructs. If one suits your requirements better than the other, prefer it as a matter of course.
Array[a,b,c] is a three dimensional, rectangular array. This means that all elements exist, but depending on your needs, may be sparsely populated. Array[a][b][c] is a three dimension jagged array, or an array of arrays of arrays.
All things being equal, I believe I once read a source (That I considered authoritative at the time) that the jagged array is generally preferable to the rectangular array. I don’t recall whether the reason was some sort of performance-related reason (caching?) or something else.