C
C#5d ago
ZHYAKO

✅ This label has not been referenced error by "defualt:"

this code sample about switch case
using System;

class TestProject8

{
        public static void Main(string[] args)

        {

                int employeeLevel = 200;

                string employeeName = "John Smith";



                string title = "";



                switch (employeeLevel)

                {

                        case 100:

                                title = "Junior Associate";

                                break;



                        case 200:

                                title = "Senior Associate";

                                break;



                        case 300:

                                title = "Manager";

                                break;



                        case 400:

                                title = "Senior Manager";

                                break;



                        defualt:

                                title = "Associate";

                }



                Console.WriteLine($"{employeeName}, {title}");

        }

}
using System;

class TestProject8

{
        public static void Main(string[] args)

        {

                int employeeLevel = 200;

                string employeeName = "John Smith";



                string title = "";



                switch (employeeLevel)

                {

                        case 100:

                                title = "Junior Associate";

                                break;



                        case 200:

                                title = "Senior Associate";

                                break;



                        case 300:

                                title = "Manager";

                                break;



                        case 400:

                                title = "Senior Manager";

                                break;



                        defualt:

                                title = "Associate";

                }



                Console.WriteLine($"{employeeName}, {title}");

        }

}
generated these warnings warning CS0162 and CS0164 on "defualt:" line warning CS0162: unreachable code detected warning CS0164: This label has not been referenced MSDN
// CS0164.cs
// compile with: /W:2
public class Program {
public static void Main() {
int i = 0;
l1: // CS0164
i++;
// Uncomment the following lines to resolve this warning.
// if (i < 10)
// goto l1; }
// CS0164.cs
// compile with: /W:2
public class Program {
public static void Main() {
int i = 0;
l1: // CS0164
i++;
// Uncomment the following lines to resolve this warning.
// if (i < 10)
// goto l1; }
no explanation about l1: could be find on google or any documentations. 🙂 Please if someone could explain why defualt: generated those two errors (warnings) I would be grateful. Thanks in advance XO
3 Replies
Tvde1
Tvde15d ago
It's a typo! It should be default:
ティナ
ティナ5d ago
the syntax Identifier: is labaled statement, used with goto because you dont have any goto to defualt (which is typo) and the statement placed after break; so the compiler warn you unused label and unreachable code
ZHYAKO
ZHYAKOOP5d ago
Thank you. I wanted to know why the compiler recognizes it as a label, and what even is a label in that context Thanks Thank you very much.

Did you find this page helpful?