Csharp/CSharp Tutorial/Statement/Foreach
Search an array using foreach.
using System;
class MainClass {
public static void Main() {
int[] nums = new int[10];
int val;
bool found = false;
for(int i = 0; i < 10; i++)
nums[i] = i;
val = 5;
foreach(int x in nums) {
if(x == val) {
found = true;
break;
}
}
if(found)
Console.WriteLine("Value found!");
}
}
Value found!
Use foreach on a two-dimensional array.
using System;
class MainClass {
public static void Main() {
int sum = 0;
int[,] nums = new int[3,5];
for(int i = 0; i < 3; i++)
for(int j=0; j < 5; j++)
nums[i,j] = (i+1)*(j+1);
foreach(int x in nums) {
Console.WriteLine("Value is: " + x);
sum += x;
}
Console.WriteLine("Summation: " + sum);
}
}
Value is: 1 Value is: 2 Value is: 3 Value is: 4 Value is: 5 Value is: 2 Value is: 4 Value is: 6 Value is: 8 Value is: 10 Value is: 3 Value is: 6 Value is: 9 Value is: 12 Value is: 15 Summation: 90
Use the foreach loop.
The foreach loop is used to cycle through the elements of a collection.
The general form of foreach is shown here:
foreach(type var-name in collection)
statement;