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.
1 2 3 4 5 6 7 8 |
var firstNames = new List<string>() { "Emma", "Olivia", "Ava", "Michael" }; foreach (string firstName in firstNames) { Console.WriteLine(firstName); } |
Example 2: Following examples show foreach loop and how it iterates.
1 2 3 4 5 6 7 8 9 10 |
var fibNumbers = new List<int> { 0, 1, 1, 2, 3, 5, 8, 13 }; int count = 0; foreach (int element in fibNumbers) { count++; Console.WriteLine($"Element #{count}: {element}"); } Console.WriteLine($"Number of elements: {count}"); |
Example 3:
1 2 3 4 5 6 7 8 |
string message = "Hello World"; foreach (var letter in message) { Console.WriteLine(letter); } |
Example 4:
1 2 3 4 5 6 7 8 9 |
IEnumerable<int> numbers = from value in Enumerable.Range(0, 50) select value; foreach (int number in numbers) { Console.WriteLine(number); } |
Example 5: Implementing IEnumerable using yield return statement is super easy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class Program { static void Main() { foreach (int number in Numbers()) { Console.Write(number+ " "); } Console.ReadLine(); } public static IEnumerable Numbers() { yield return 10; yield return 20; yield return 30; } } |
Example 6: Iterates objects in a List
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
class Book { string title; string genre; int price; public string Title { get => title; set => title = value; } public string Genre { get => genre; set => genre = value; } public int Price { get => price; set => price = value; } public Book(string t, string g, int p) { title = t; genre = g; price = p; } } class Program { static void Main(string[] args) { List<Book> books = new List<Book>(); books.Add(new Book("Broken hearts", "Romance", 15)); books.Add(new Book("Nothing in my life", "Guide", 12)); books.Add(new Book("Love for love", "Romance", 6)); foreach(var book in books) { Console.WriteLine("Book : {0}\t Genre : {1} \t Price : {2} \t", book.Title, book.Genre, book.Price); } Console.ReadLine(); } } |
Example 7:Custom implementating of IEnumerable is not so easy. You have to implement interface IEnumerable (one method GetEnumerator) and interface IEnumerator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
class Book { string title; string genre; int price; public string Title { get => title; set => title = value; } public string Genre { get => genre; set => genre = value; } public int Price { get => price; set => price = value; } public Book(string t, string g, int p) { title = t; genre = g; price = p; } } class Books:IEnumerable { List<Book> BookList = new List<Book>(); public void Add(Book b) { BookList.Add(b); } public IEnumerator GetEnumerator() { return BookList.GetEnumerator(); } } class Program { static void Main(string[] args) { Books books = new Books(); books.Add(new Book("Broken hearts", "Romance", 15)); books.Add(new Book("Nothing in my life", "Guide", 12)); books.Add(new Book("Love for love", "Romance", 6)); foreach (Book book in books) { Console.WriteLine("Book : {0}\t Genre : {1} \t Price : {2} \t", book.Title, book.Genre, book.Price); } Console.ReadLine(); } } |