Question

For this program:

class Program
{
    static void Main(string[] args)
    {
        var state = States.One;
        switch (state)
        {
            case States.One:
                Console.WriteLine("One");
                break;
            case States.Zero:
                goto case States.One;
        }
    }
}

public enum States : ulong
{
    Zero = 0,
    One = 1,
}

I got:

"A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type"

But state variable is enum type. The error disappears if I comment the goto case line.

I am using VS 2013. + .NET 4.5.1.

Was it helpful?

Solution

This is known bug of the C# compiler when enum is typed as ulong and you use goto case at the same time. If you remove the ulong from enum, it compiles just fine. And because not many people run into this problem, they are not focusing on fixing it.

OTHER TIPS

Depending on your use case, this might also be an option for you:

switch (state)
{
    case States.Zero:
    case States.One:
        Console.WriteLine("One");
        break;
}

This should be working according to an example here: http://msdn.microsoft.com/de-de/library/06tc147t.aspx

You could use a label for goto instead of using the case directly in the goto statement:

switch (state)
{
    case States.One:
caseZeroRedirect:
        Console.WriteLine("One");
        break;
    case States.Zero:
        CouldDoSomethingFirst();
        goto caseZeroRedirect;
}

You should try this:-

switch (state)
{
 case States.Zero:
 case States.One:
    Console.WriteLine("1");
    break;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top