Module 3 · Collections and objects: organise your data · Lesson 9 of 20
Keep several values together with arrays
Three study topics belong together. How can the program print the first one and visit all three? Start by distinguishing an index from the value stored there.
Predict the study topics
We have used one variable for one value. An array groups several values of the same element type. string[] means an array of strings. Predict the three topic lines and the final count before running.
using System;
string[] topics = { "Variables", "Loops", "Methods" };
topics[1] = "Input";
foreach (string topic in topics)
{
Console.WriteLine(topic);
}
Console.WriteLine($"Count: {topics.Length}");Which topic is replaced by Input?
Show answer
Correct answer: B
Array indexes begin at zero. Index 1 is the second position, which initially contains Loops.
Option A: Variables is at index 0.
Option C: Methods is at index 2.
Option D: The assignment targets one position.
Hint: Label the positions 0, 1 and 2.
See positions and contents separately
| Index | Value before assignment | Value after topics[1] = "Input" |
|---|---|---|
| 0 | Variables | Variables |
| 1 | Loops | Input |
| 2 | Methods | Methods |
An index is a position, not the value stored at that position. An array of length three has valid indexes 0, 1 and 2. There is no position 3. The length is fixed for this array instance, although the values in its positions can change.
foreach visits the elements in order. On each pass, topic is the current element value. We use that when we only need to read each value. The loop manages traversal, so you do not have to calculate a valid position yourself.
Read topics.Length as “the number of elements in topics.” It is a property, so there are no parentheses after Length. Do not confuse the count with the last valid index: the last index is Length - 1 when the array is non-empty.
For an array with three elements, which access is outside its bounds?
Show answer
Correct answer: D
The valid indexes run from 0 through 2. Accessing index 3 would fail at run time.
Option A: Index 0 is the first element.
Option B: Index 1 is the second element.
Option C: Index 2 is the last valid element.
Hint: The number of positions is three, but numbering begins at zero.
Use for when the position matters
The next program adds study minutes. The condition is index < minutes.Length, so the loop stops before an invalid index. It works with the actual array length instead of a separately typed number that could become outdated.
using System;
int[] minutes = { 15, 20, 25 };
int total = 0;
for (int index = 0; index < minutes.Length; index++)
{
total += minutes[index];
}
Console.WriteLine($"Total: {total}");
Console.WriteLine($"First: {minutes[0]}");
Console.WriteLine($"Last: {minutes[minutes.Length - 1]}");Which condition correctly visits every element with an index starting at 0?
Show answer
Correct answer: B
Strictly less than Length allows the last valid index and excludes Length itself.
Option A: The <= condition also allows one invalid position at Length.
Option C: Starting at zero, this condition is false for a non-empty array.
Option D: Starting at zero, this does not enter the loop for this array.
Try it independently
Given 15, 20 and 25 minutes, count how many days reached at least 20 minutes. Use a foreach loop and an if statement. Start the count at zero, and add one only when a value meets the boundary. Expect Days meeting goal: 2.
Write your version first. This complete solution combines two ideas you already know rather than introducing another collection operation.
using System;
int[] minutes = { 15, 20, 25 };
int daysMeetingGoal = 0;
foreach (int dayMinutes in minutes)
{
if (dayMinutes >= 20)
{
daysMeetingGoal++;
}
}
Console.WriteLine($"Days meeting goal: {daysMeetingGoal}");Explain back
Explain why the first array element has index 0 but Length is 3. Change the minutes to 20, 20 and 5: predict two qualifying days again. Then use an empty array initializer and predict zero; a foreach loop over an empty array has no iterations.
Reference: Microsoft — arrays.
Quick reference
- type[] declares an array element type.
- The first index is 0.
- Length is the element count, not the last valid index.
- Array length stays fixed for that instance.
- Use foreach to read each element.
- For indexes, use index < array.Length.
TechInterviewNotes Editorial Team · Updated 27 Oct 2026 ·