How to use switch with goto case in C#

1 Answer

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "abcd";

            for (int i = 0; i < s.Length; i++)
            {
                  switch (s[i])
                  {
                        case 'a':
                            Console.Write("a - not ");
                            goto case 'x';
                        case 'b':
                            Console.Write("b - not ");
                            goto case 'x';
                        case 'x':
                            Console.WriteLine("x");
                            break;
                        default:
                            Console.WriteLine("default");
                            break;
                  }
             }
         }
    }
}


/*
run:
    
a - not x
b - not x
default
default

*/

 



answered Apr 25, 2017 by avibootz

Related questions

1 answer 247 views
1 answer 298 views
2 answers 388 views
2 answers 375 views
3 answers 431 views
2 answers 360 views
3 answers 384 views
...