๐ 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.
.Length
property 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.Length
for 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 :