Search an array using foreach in CSharp
Description
The following code shows how to search an array using foreach.
Example
using System; // w ww . j a v a2 s. c o m
public 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!");
}
}
The code above generates the following result.