get out of foreach statement csharp

Code Example - get out of foreach statement csharp

                
                        foreach (string s in sList)
{
    if (s.equals("ok"))
    {
        break; // get out of the loop
    }
}
                    
                
 

javascript foreach

                        
                                const avengers = ['thor', 'captain america', 'hulk'];
avengers.forEach((item, index)=>{
	console.log(index, item)
})
                            
                        
 

foreach javascript

                        
                                var items = ["item1", "item2", "item3"]
var copie = [];

items.forEach(function(item){
  copie.push(item);
});
                            
                        
 

csharp foreach

                        
                                var numbers = new NumberList<int> { 0, 1, 2, 3, 4, 5 };
foreach (int num in numbers)
{
    DoSomething();
}
                            
                        
 

csharp foreach namevaluecollection

                        
                                NameValueCollection nv = HttpUtility.ParseQueryString(queryString);        
foreach (string key in nv) {
    var value = nv[key];

}
                            
                        
 

for each csharp

                        
                                foreach (int element in fibNumbers)
{
    Console.Write(%%%~COMPRESS~PRE~5~%%%quot;{element} ");
}
                            
                        
 

csharp break from foreach method

                        
                                list.Foreach((item) => {
	// You cannot break from here beacuse of the structure of the method.
  	// Underneath is they way the meothd is tructured and a break is not allowed.
	//public static ForEach<T>(this IEnumerable<T> input, Action<T> action)
	//{
  	//foreach(var i in input)
    //	action(i);
	//}
});

// You have to convert your code into the traditional foreach:
foreach(var item in list) {
	// Your code here
  	break; // <- As you can see you can break here.
}
                            
                        
 

csharp exit foreach

                        
                                Use break