Foreach C# Là Gì

     

In this article, we will learn about foreach loops (an alternative to for loop) and how khổng lồ use them with arrays and collections.

Bạn đang xem: Foreach c# là gì


C# provides an easy to lớn use & more readable alternative to for loop, the foreach loop when working with arrays & collections to lớn iterate through the items of arrays/collections. The foreach loop iterates through each item, hence called foreach loop.

Before moving forward with foreach loop, visit:

C# arraysC# collections

Syntax of foreach loop

foreach (element in iterable-item) // toàn thân of foreach loopHere iterable-item can be an array or a class of collection.

How foreach loop works?

*
Working of C# foreach loop

The in từ khóa used along with foreach loop is used to iterate over the iterable-item. The in keyword selects an thành phầm from the iterable-item on each iteration và store it in the variable element.

On first iteration, the first nhà cửa of iterable-item is stored in element. On second iteration, the second element is selected and so on.

The number of times the foreach loop will execute is equal lớn the number of elements in the array or collection.

Here is an example of iterating through an array using the for loop:

Example 1: Printing array using for loop

using System; namespace Loop{ class ForLoop { public static void Main(string<> args) { char<> myArray = "H","e","l","l","o"; for(int i = 0; i The same task can be done using the foreach loop.

Example 2: Printing array using foreach loop

using System; namespace Loop class ForEachLoop public static void Main(string<> args) char<> myArray = "H","e","l","l","o"; foreach(char ch in myArray) Console.WriteLine(ch); When we run the both program, the output đầu ra will be:

HelloIn the above program, the foreach loop iterates over the array, myArray. On first iteration, the first element i.e. MyArray<0> is selected and stored in ch.

Xem thêm: Top 5+ Ổ Cứng Ssd Tốt Nhất 2017, 5 Tiêu Chí Chọn Ổ Cứng Ssd Tốt Nhất


Similarly on the last iteration, the last element i.e. MyArray<4> is selected. Inside the body toàn thân of loop, the value of ch is printed.

When we look at both programs, the program that uses foreach loop is more readable & easy to understand. This is because of its simple & expressive syntax.

Hence, foreach loop is preferred over for loop when working with arrays and collections.

Example 3: Traversing an array of gender using foreach loop

This program computes the number of male và female candidates.

using System; namespace Loop class ForEachLoop public static void Main(string<> args) char<> gender = "m","f","m","m","m","f","f","m","m","f"; int male = 0, female = 0; foreach (char g in gender) if (g == "m") male++; else if (g =="f") female++; Console.WriteLine("Number of male = 0", male); Console.WriteLine("Number of female = 0", female); When we run the program, the đầu ra will be:

Number of male = 6Number of female = 4

Example 4: foreach loop with các mục (Collection)

This program computes the sum of elements in a List.

using System;using System.Collections.Generic;namespace Loop class ForEachLoop public static void Main(string<> args) var numbers = new List() 5, -8, 3, 14, 9, 17, 0, 4 ; int sum = 0; foreach (int number in numbers) sum += number; Console.WriteLine("Sum = 0", sum); Console.ReadLine(); When we run the program, the output đầu ra will be:

Sum = 44In this program, foreach loop is used to traverse through a collection. Traversing a collection is similar to traversing through an array.

Xem thêm: Phần Mềm Thu Âm Giọng Hát Trên Iphone, Xin Phần Mềm Thu Âm Chất Lượng Trên Ios

The first element of collection is selected on the first iteration, second element on second iteration and so on till the last element.