C# Foreach Loop Examples – C# Programming Example
C# Console Foreach Statement

C# Foreach Loop Examples

Foreach loop is used to access elements of a list quickly without performing initialization, testing and increment/decrement.  The working of foreach loops is to do something for every element rather than doing something n times.

In this article, we will show you how to loop List with the  foreach statement.

 

Example 1: Following examples show foreach loop and how it iterates.

 

Example 2: Following examples show foreach loop and how it iterates.

Example 3:

Example 4:

Example 5: Implementing IEnumerable using yield return statement is super easy.

 

Example 6: Iterates objects in a List

 

Example 7:Custom implementating of IEnumerable is not so easy. You have to implement interface IEnumerable (one method GetEnumerator) and interface IEnumerator

 

 

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: