How to exit a foreach loop in C#

1 Answer

0 votes
using System;
 
class ExitForeachLoop_CSharp
{
    static void Main(string[] args)
    {
        string s = "c# programmer";
 
        foreach (char ch in s) {
            if (ch == 'g') {
                break;
            }
            Console.WriteLine(ch);
        }
    }
}
 

 
/*
run:
      
c
#
 
p
r
o
 
*/

 



answered Aug 8, 2024 by avibootz

Related questions

1 answer 182 views
182 views asked Apr 21, 2017 by avibootz
3 answers 225 views
1 answer 157 views
157 views asked Mar 6, 2023 by avibootz
2 answers 195 views
195 views asked Nov 25, 2020 by avibootz
1 answer 199 views
1 answer 201 views
2 answers 293 views
...