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

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...