C# Arrays β Master Fixed-Size Collections in C#
Introduction β Why Learn Arrays in C#?
Arrays are one of the fundamental building blocks of any programming language. In C#, arrays allow you to store multiple elements of the same data type using a single variable name and access them by index. They’re ideal when working with static datasets like top scores, temperature logs, or fixed-form inputs.
In this guide, youβll learn:
- What arrays are and how they work
- Array declaration, initialization, and traversal
- Multi-dimensional and jagged arrays
- Best practices, examples, and real-world use cases
Core Concept β What Is an Array in C#?
An array is a fixed-length collection that holds elements of the same type. The size is defined at creation and cannot change afterward.
Syntax:
data_type[] arrayName = new data_type[size];
Example:
int[] numbers = new int[3];
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
Code Example β Array Declaration and Access
string[] fruits = { "Apple", "Banana", "Cherry" };
for (int i = 0; i < fruits.Length; i++)
{
Console.WriteLine(fruits[i]);
}
Output:
Apple
Banana
Cherry
Explanation:
- Array is initialized with 3 strings.
.Lengthproperty gets total number of elements.- Loop prints each item by index.
Traversing Arrays β Foreach Loop
int[] scores = { 90, 85, 78 };
foreach (int score in scores)
{
Console.WriteLine(score);
}
Use Case: Read-only access to elements with simpler syntax.
Multi-Dimensional Arrays
int[,] matrix = new int[2, 3] { {1, 2, 3}, {4, 5, 6} };
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write($"{matrix[i, j]} ");
}
Console.WriteLine();
}
Output:
1 2 3
4 5 6
Use Case: Matrices, tables, spreadsheets, game boards
Jagged Arrays (Array of Arrays)
int[][] jagged = new int[2][];
jagged[0] = new int[] { 1, 2 };
jagged[1] = new int[] { 3, 4, 5 };
foreach (int[] row in jagged)
{
foreach (int val in row)
{
Console.Write(val + " ");
}
Console.WriteLine();
}
Output:
1 2
3 4 5
Use Case: Irregular tables, variable-length rows
Best Practices & Tips
Tip: Always check .Length to avoid IndexOutOfRangeException.
Pitfall: Arrays are zero-indexed; the first element is at index 0.
Best Practice: Use foreach when you donβt need to modify array contents by index.
Comparison β Arrays vs Lists
| Feature | Array ([]) | List (List<T>) |
|---|---|---|
| Size | Fixed | Dynamic |
| Performance | Faster for fixed-size data | More flexible |
| Syntax | Simple | Requires System.Collections.Generic |
| Use Case | Static datasets | Dynamic collections |
Real-World Use Cases
- Storing game high scores
- Performing matrix operations
- Handling fixed-size form inputs
- Mapping 2D grid-based layouts
- Collecting sensor data with a known sample size
Summary β Recap & Next Steps
Key Takeaways:
- Arrays hold fixed-size, same-type data.
- Use indexers (
array[i]) and.Lengthfor access. - C# supports 1D, multi-dimensional, and jagged arrays.
Real-world relevance: Arrays are core to data processing, grid-based UIs, game development, and static data storage.
FAQ Section
Are C# arrays zero-based?
Yes, the index starts at 0. array[0] accesses the first element.
Can I resize an array in C#?
No. Arrays are fixed-size. Use List<T> for dynamic resizing.
What is the difference between int[] and int[,]?
int[] is a single-dimensional array. int[,] is a multi-dimensional (rectangular) array.
How do I find the length of an array?
Use .Length:
int length = array.Length;
Can I return an array from a method?
Yes:
int[] GetNumbers() => new int[] {1, 2, 3};
Share Now :
