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.

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...