C# Error CS0153 – A goto case is only valid inside a switch statement

C# Compiler Error

CS0153 – A goto case is only valid inside a switch statement

Reason for the Error

You will receive this error when you have used the goto case statement in C# outside of a switch statement.

For example, the below code snippet will result in the error code CS0153 because goto case 1 is used outside of the switch statement with-in the main function.

namespace ConsoleApp2
{
    class Program
    {
        public static void Main()
        {
            goto case 1;
            int value = 1;
            switch (value)
            {
                case 1:
                    break;

            }
        }
    }
}

Error CS0153 A goto case is only valid inside a switch statement ConsoleApp2 C:\Users\Senthil\source\repos\ConsoleApp1\ConsoleApp2\Program.cs 8 Active

C# Error CS0153 – A goto case is only valid inside a switch statement

Solution

Goto case can only be used with-in switch statement in C#. If you are using goto case outside of the switch statement, you’ll need to remove that statement.