The foreach Loop
Iterate without indices.
The foreach Loop is a free C# Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is foreach?
The foreach loop lets you visit every item in a collection one by one.
You don't manage an index or a counter. C# walks the collection for you and hands each element to a variable you name.
It is the cleanest way to read all items in an array or list.
Basic Syntax
The shape is simple: foreach (var item in collection) followed by a body.
On each pass, item holds the next element. The loop ends automatically after the last one.
foreach (var name in names)
{
Console.WriteLine(name);
}Your First foreach
Let's loop over an array of strings and print each name.
Notice there is no index and no length check. The loop simply visits every element in order.
string[] names = { "Ada", "Linus", "Grace" };
foreach (var name in names)
{
Console.WriteLine(name);
}The Loop Variable
The loop variable (here n) is a fresh copy created for each item.
You can use any name you like. Pick something that describes the element, like number or n.
int[] scores = { 90, 75, 88 };
foreach (var n in scores)
{
Console.WriteLine(n);
}Using var vs Explicit Type
You can write the element type explicitly instead of var.
Both work the same. var lets the compiler infer the type, which keeps the code short.
int[] scores = { 90, 75, 88 };
foreach (int score in scores)
{
Console.WriteLine(score);
}Doing Work in the Body
The loop body can do anything: calculations, conditions, or building a total.
Here we add each value to a running sum and print the result after the loop.
int[] nums = { 4, 8, 15 };
int total = 0;
foreach (var x in nums)
{
total += x;
}
Console.WriteLine(total);foreach with Conditions
You can put an if inside the loop to act only on certain items.
This example prints only the even numbers from the array.
int[] nums = { 1, 2, 3, 4, 5, 6 };
foreach (var x in nums)
{
if (x % 2 == 0)
Console.WriteLine(x);
}foreach Reads, Not Replaces
The loop variable is read-only inside the loop. You cannot reassign it to change the collection.
This line would cause a compile error, because the foreach variable is immutable.
foreach (var x in nums)
{
x = x + 1; // ERROR: cannot assign to iteration variable
}Looping Over Characters
A string is a sequence of characters, so you can foreach over it too.
Each pass gives you one char from the text.
string word = "code";
foreach (var c in word)
{
Console.WriteLine(c);
}When to Use foreach
Use foreach when you want to visit every element and you don't need the index.
If you need the position, or want to modify the collection while looping, a for loop is a better fit.
foreach vs for
A for loop manages a counter and uses indexing like arr[i].
foreach hides that machinery. It is shorter and avoids off-by-one mistakes when you just need to read each item.
for (int i = 0; i < names.Length; i++)
{
Console.WriteLine(names[i]);
}Quick Check
Test what you learned about the foreach loop.
Recap
You learned that foreach visits every element of a collection without an index.
The loop variable is a read-only copy of each item. Use foreach for clean reads, and for when you need the position or want to modify items.
Frequently asked questions
Is the “The foreach Loop” lesson free?
Yes — the full text of “The foreach Loop” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “The foreach Loop”?
Iterate without indices. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The foreach Loop” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The foreach Loop
- Iterating Collections
- break and continue
- Common Iteration Mistakes