C# Error CS0163 – Control cannot fall through from one case label (‘label’) to another

C# Compiler Error

CS0163 – Control cannot fall through from one case label (‘label’) to another

Reason for the Error

You will receive this error when you DONOT explicitly terminate a switch statement in C#.

For example, try compiling the below code snippet.

using System;

namespace ConsoleApp2
{
    class Program
    {
        public static void Main()
        {
            int val = 10;
            switch(val)
            {
                case 1: Console.WriteLine("Case 1");
                case 2: break;
            }
        }

    }
    
}

You will receive the error code CS0163 in C# for the above code snippet because the section (case 1 ) is not terminated explicitly using break.

C# Error CS0163 – Control cannot fall through from one case label ('label') to another

Error CS0163 Control cannot fall through from one case label (‘case 1:’) to another ConsoleApp2 C:\Users\Senthil\source\repos\ConsoleApp1\ConsoleApp2\Program.cs 12 Active

Solution

When the switch statement contains multiple cases, it will need to be terminated by one of the following keywords : return, goto, break, throw, continue.

The error in the above code can be fixed by adding break to terminate the case 1 as show below.

using System;

namespace ConsoleApp2
{
    class Program
    {
        public static void Main()
        {
            int val = 10;
            switch(val)
            {
                case 1: Console.WriteLine("Case 1");
                    break;
                case 2: break;
            }
        }

    }
    
}

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...