C# Loops Deep Dive

Difficulty: Intermediate
Duration: 1 minute and 14 seconds
Students: 575
Rating: 5/5

Structures to repeat tasks or access data collections are at the heart of compact and reusable code. C#, like other programming languages, provides three basic mechanisms allowing you to execute statements multiple times dynamically. In this lesson, we'll take an in-depth look at the for loop, while loop, and foreach loop.

These looping mechanisms vary in structure and intended application, and we'll examine how to use them in different scenarios. In the lesson of this detailed investigation, you will learn some of the potential pitfalls that accompany programming with each type of loop, how to avoid them, and create efficient loops. There are code examples demonstrating the use of each loop type in a practical way, along with other helpful C# and .NET code snippets.

Learning Objectives

  • An in-depth understanding of for loops
  • Learn about while loop syntax and see how they are used
  • Gain a foundational understanding of object lists
  • Learn how to use, and not to use, a foreach loop to iterate through a list

Intended Audience

This lesson is intended for those who already have a basic understanding of C# and want to learn about loops.

Prerequisites

To get the most out of this lesson, you should have a basic knowledge of C# as well as an understanding of object orientation and C# classes. Please consider taking our Introduction to Object Orientation and C# Classes lesson before taking this one.

Source Code

Source code related to the lectures can be found here

Covered Topics