C# Multidimensional Arrays
C# Multidimensional Arrays
A multidimensional array in C# is an array with more than one dimension. The most common type is a 2D array (rows and columns), similar to a table or matrix.
Declaring a Multidimensional Array
Initializing a 2D Array
Accessing Elements
📌 Index starts from 0 for both rows and columns.
Loop Through a 2D Array
3D Array Example
Loop Through 3D Array
Multidimensional vs Jagged Arrays
| Feature | Multidimensional | Jagged |
|---|---|---|
| Structure | Rectangular | Array of arrays |
| Syntax | int[,] | int[][] |
| Row size | Same | Can vary |
| Performance | Slower | Faster |
When to Use
✔ Tables
✔ Matrices
✔ Grid-based data
✔ Game boards
Common Mistakes
❌ Wrong index order
❌ Using Length instead of GetLength()
❌ Index out of range
Summary
✔ Use [,] for multidimensional arrays
✔ Use GetLength(dimension)
✔ Supports 2D, 3D, and more
✔ Index starts from 0
