Lesson 14.3 – Foreach loops

Lesson Objectives

At the end of this lesson, you will know…

  • How to use a “foreach” to work with the objects inside lists


Another common thing you’ll need to do is look at the objects contained in a list. Maybe you’ll need to total up some values, or see if you have a certain object in the list.

This is where you can use “foreach” loop. Think of it as, “I want to do ‘something’ for each object in this list.”


Using a foreach to see if an item exists in a list

In the World class, we already have some functions with foreach.

Here’s the code for the ItemByID() function that finds an item, based on its ID:

We already have the Items list created as a class-level variable and populated from the constructor. This function will find the item with the same ID as the parameter that was passed in, and return it to the part of the program that called this function.

Here is what’s happening in the parentheses after the “foreach”:

The “in Items”, at the end, is where you say which list you want to look through. In this case, it’s the “Items” list.

The “Item” is the datatype of the objects in this list.

And, the “item” is the variable that will hold each object from the list, when the code between the curly braces is run.

So, the general pattern for a foreach is:

foreach(DataType variableName in listName)

When this code runs, it will take the first object from the “Items” list, and assign it to the “item” variable.

Then it will check to see if the “ID” property of the current “item” variable matches the “id” that was passed in as a parameter to the function. If it does match, the function returns that item object. If it doesn’t match, the foreach will get the next object from the list, assign it to the “item” variable, and then check it.

If the function goes through all the item objects in the Items list, and none of them match, the “foreach” loop will finish and the rest of the code in the function will be run.

In this case, the only code in the rest of the function is “return null” (nothing/empty).

Note 1: Returning null is generally not a good idea. The code that called this function is expecting an “Item” object returned, and now it also needs to handle receiving a null.

This is one of those things we’re doing to keep it simple for these tutorials that you probably wouldn’t do in a “real” program.


Note 2: .Net has something called LINQ that also works with lists. It lets you perform the same logic, but often in a single line of code. However, for some people, it takes a little time to figure out how to work with it.

Here’s a function to show you how LINQ can be much shorter than using a foreach.

In the first few lines, we create a list of “double”s (double-precision numbers, that can have decimal values) and add values to it.

Next, we calculate the average value with a “foreach”.

To do this, we need to have some variables to hold the total of the values in the list and to see how many items are in the list. Then we calculate the average by dividing the total by the number of values (for this sample, I’m not worrying about the list being empty, which would result in a “divide by zero” error).

That code takes up 10 lines in the function.

The final line is how you can get the average value from a list, using LINQ. It’s one line.

This is a simple demo of one LINQ function. They can get more complex, which is why I’m not covering them in this tutorial.



Now you can work with the lists we have in the game – the player’s inventory and quest lists, and the list of locations, items, monsters, and quests in the World class.


Source code for this lesson

There is no code for this lesson


Next lesson: Lesson 15.1 – Getting random numbers for the game

Previous lesson: Lesson 14.2 – If statements

All lessons: Learn C# by Building a Simple RPG Index

2 thoughts on “Lesson 14.3 – Foreach loops

  1. Hello,

    Is there a place where I could learn LINQ. In my understanding, LINQ has its own syntax and you can query multiple data sources. Correct me if I am wrong.

    1. A good resource is 101 LINQ Samples in C# – although, it uses the SQL-like format for its LINQ statements, and I prefer to use the lambda format (that is only my personal preference). You can use LINQ to query multiple data sources and do things such as Group, Distinct, Intersect, and Except.

Leave a Reply

Your email address will not be published. Required fields are marked *