Skip to content
AgileLogics
4 min read

Arrays

Prerequisites: Loops

int[] scores = { 90, 85, 77 };
Console.WriteLine(scores[0]);   // 90
Console.WriteLine(scores.Length); // 3 — a property, no parentheses

scores[1] = 88;

Unlike a plain C# array (fixed-size, like Java's), List<T> (covered next in Level 2) can grow and shrink — reach for it whenever the number of elements isn't fixed in advance.

Common mistakes

  • Accessing an index equal to or beyond Length — throws IndexOutOfRangeException immediately.

Practice

Declare an int array of 5 numbers, and print their sum using a foreach loop.

Show solution
int[] nums = { 1, 2, 3, 4, 5 };
int sum = 0;
foreach (int n in nums) {
    sum += n;
}
Console.WriteLine(sum); // 15