Looping though lists to make decisions

These previous array, List, and Dictionary examples showed how to get data into them, and how they store data. It's now time to learn how to loop through the data to retrieve the needed data.

Here are some common ways to perform loops:

  • foreach loop
  • for loop
  • while loop

Using the foreach loop

When working with Collections such as an array, a list or dictionary, the preferred way to cycle through the elements and retrieve data is to use the foreach loop.

Get Learning C# by Developing Games with Unity 3D Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.