C# Iterating
Keep a List<int> named bag. Add 3, then add 8. Write a foreach loop that prints each value in bag. Keep the work in Pine with Main. Keep using System.Collections.Generic; under using System;. Run with dotnet run. The screen should show two lines: 3 then 8. foreach walks the collection one value at a time. The loop variable holds the current value. You do not write bag[0] and bag[1] by hand for this walk. Keep the starting public class named Pine.
Walk each value in turn
On the hashset page you added unique tags and read Count. This page walks values one by one. A foreach loop visits every item in a collection. For a List<int> named bag, each step gives you the next int. For a HashSet<string> named tags, each step gives you the next string. The order of a set walk is not the teaching focus here — seeing each value print is enough.
Open the Pine file. The starting class name stays Pine. This page does not rename the starter. Delete any leftover that only prints tags.Count after three Adds with no foreach. Delete any Dictionary indexer leftover. Delete any ReadLine line. The file you keep has no wait for typing. A clean Main makes the two-line foreach screen easy to trust.
At the top keep using System;. Keep using System.Collections.Generic;. Inside Main, write List<int> bag = new List<int>();, then bag.Add(3);. For a first check, write:
foreach (int n in bag)
{
Console.WriteLine(n);
}
Save and run that short shape, then extend to two Adds as the kept demo. The one-Add run is only a checkpoint; the page's kept answer is still 3 then 8.
The word after foreach in parentheses is the loop variable type and name, then in, then the collection. Do not invent for (int i = 0; ...) as the required path on this page — stay with foreach for the walk.
using System;
using System.Collections.Generic;
public class Pine
{
public static void Main()
{
List<int> bag = new List<int>();
bag.Add(3);
foreach (int n in bag)
{
Console.WriteLine(n);
}
}
}
Pine walks two list values
Inside Main, after the empty list, write bag.Add(3); then bag.Add(8);. Keep the same foreach (int n in bag) block that writes n. You are not only printing tags.Count on this page. The kept demo walks the list and shows each number.
using System;
using System.Collections.Generic;
public class Pine
{
public static void Main()
{
List<int> bag = new List<int>();
bag.Add(3);
bag.Add(8);
foreach (int n in bag)
{
Console.WriteLine(n);
}
}
}
Read the kept file from the top. using System; prepares console names. using System.Collections.Generic; prepares List. public class Pine is the starter. public static void Main() is where the runtime starts. new List<int>() builds an empty list. The two Add calls store 3 then 8. The foreach visits 3 first, then 8. Each visit writes one line. The screen shows 3 then 8.
Run it with dotnet run. The program writes two lines and stops. It does not wait for you to type. If dotnet run waits, ReadLine is still in the file. Delete that line and keep the foreach work. The screen shows 3 then 8. If the screen shows only 2, a leftover hashset Count demo may still be running. If the build cannot find List, restore using System.Collections.Generic;.
If you write only Console.WriteLine(bag[0]); without foreach, you miss the second value on purpose for this lesson — keep the loop. If the list is empty, foreach writes nothing. Add the values before the loop on the kept demo.
foreach on a HashSet too
The same loop shape works on a set. Create HashSet<string> tags, add "oak" and "pine", then foreach (string t in tags) and write t. You see each tag on its own line. Order may differ from a list; this page only requires that both words appear. Use this as a second checkpoint, then return to the list foreach file when you want 3 then 8 on screen for the quiz.
using System;
using System.Collections.Generic;
public class Pine
{
public static void Main()
{
HashSet<string> tags = new HashSet<string>();
tags.Add("oak");
tags.Add("pine");
foreach (string t in tags)
{
Console.WriteLine(t);
}
}
}
Run the HashSet foreach file with dotnet run. You should see oak and pine each on a line (order can vary). If you only see 2, you still have a Count-only leftover — switch to the foreach body. Do not treat foreach as a replacement for every Count check. Count still answers how many. foreach answers by visiting each value.
Writing foreach (int n in bag) when bag is a HashSet<string> fails to build — the loop variable type must match the collection's value type. Keep int with List<int> and string with HashSet<string> on this page.
Compared with Count-only and with a fixed index habit
Printing tags.Count after Adds tells you the length. Walking with foreach shows the values themselves. A leftover Count-only hashset demo is a different checkpoint. For this page keep a foreach that writes each value.
You already used index reads like bag[0] on the list page. foreach does not need you to invent the indexes by hand for a full walk. This page does not ban indexes. It shows the loop that visits every stored value in turn. Stay with small collections so the screen stays easy to read.
What this leaves for nullable
This page walks a list and a set with foreach. A later page — nullable — covers value types that can hold no value with ? and null checks. Stay with iterating collections here. Do not invent nullable syntax on this page; the lesson goal is the walk itself. When dotnet run prints 3 then 8 from the list foreach file, you have the checkpoint this page asks for.
Do not nest a second Main. Do not leave Count-only demos as the only teaching path. If the screen shows one number alone, check that WriteLine is inside the foreach body. Keep using System.Collections.Generic; for List and HashSet.
You can write List<int> bag = new List<int>();, call bag.Add(3); and bag.Add(8);, then foreach (int n in bag) { Console.WriteLine(n); }. Run with dotnet run. The screen shows 3 then 8. The loop visited each value. The same idea works on a HashSet<string> with foreach (string t in tags). Think of foreach as a walk along the collection: one step, one value, one print. Pine stays the door you open with dotnet run. The next page is nullable, where a value type can also mean "no value".
Keep using System.Collections.Generic;. Keep leftover Count-only hashset demos out of the kept list foreach file so the screen cannot show only 2 by mistake. Those habits are the checkpoint before you move on.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top