Search an array using foreach. : Foreach « Statement « C# / CSharp Tutorial






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!








4.4.Foreach
4.4.1.Use the foreach loop.
4.4.2.Use break with a foreach.
4.4.3.Use foreach on a two-dimensional array.
4.4.4.Search an array using foreach.