An array is a data structure that stores a fixed-size sequential collection of elements of the same type. In C#, arrays are reference types stored on the heap, and they are zero-indexed.
1. One-Dimensional (1D) Arrays
A 1D array is a single linear sequence of elements. It is the most common array type used for simple lists.
string[] fruits = new string[]
{
"Apple", "Banana"
};
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}Key Characteristics
-
Declaration:
string[] arrayName -
Accessing Elements:
fruits[0]returns"Apple". -
Length Property:
fruits.Lengthgives the total number of items.
2. Two-Dimensional (2D) Rectangular Arrays ([,])
Unlike a list of lists, a true rectangular 2D array forms a rigid grid or matrix. Every row has the exact same fixed number of columns.
Example Code
C#
// Declaration and initialization of a 3x2 rectangular 2D array
string[,] matrix = new string[,]
{
{ "Round", "Apple" },
{ "Long", "Banana" },
{ "Green", "Watermelon" }
};
// 1. Accessing a specific element by coordinates [row, column]
Console.WriteLine(matrix[0, 1]); // Output: Apple (Row 0, Column 1)
// 2. Iterating grid-style using GetLength()
Console.WriteLine("\n--- Grid Iteration ---");
for (int row = 0; row < matrix.GetLength(0); row++)
{
for (int col = 0; col < matrix.GetLength(1); col++)
{
Console.Write($"{matrix[row, col]} \t");
}
Console.WriteLine(); // New line after each row
}
Key Characteristics
-
Syntax: Uses commas inside the brackets, e.g.,
[,]or[,,]for 3D. -
Dimensions:
GetLength(0)returns the number of rows;GetLength(1)returns the number of columns. -
Memory: Allocated as a single continuous block of memory, making it highly cache-friendly for math and matrix operations.
3. Jagged Arrays ([][])
A jagged array is an array of arrays. Because each inner array is independent, rows can have completely different lengths.
Example Code
C#
// Jagged array with varying row lengths
string[][] jgdAr = new string[][]
{
new string[] { "Round", "Apple" },
new string[] { "Long", "Banana" },
new string[] { "Green", "Watermelon", "Pear" } // 3 items instead of 2!
};
// Iterating through a jagged array dynamically
foreach (string[] row in jgdAr)
{
// string.Join neatly formats rows of any length without index errors
Console.WriteLine($"Row items: [{string.Join(", ", row)}]");
}
Key Characteristics
-
Syntax: Uses stacked square brackets
[][]. -
Flexibility: Ideal for irregular data structures where rows contain different counts of elements.
-
Memory: Less memory-efficient than rectangular arrays for pure grids because each inner array has its own object header overhead on the heap.
Summary Comparison Table
| Feature | 1D Array ([]) | 2D Rectangular ([,]) | Jagged Array ([][]) |
|---|---|---|---|
| Structure | Single linear sequence | Rigid matrix grid | Array of independent arrays |
| Row Lengths | Uniform | Strictly uniform across all rows | Variable (can differ per row) |
| Syntax | type[] | type[,] | type[][] |
| Dimension Lookup | array.Length | array.GetLength(dimension) | array.Length (outer) / row.Length (inner) |