Search an array using foreach. : Foreach : Statement C# Examples


C# Examples » 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!");  
    }  
}
    
   
  
   



Output

Value found!


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Statement
» Foreach