Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
120 views
in Technique[技术] by (71.8m points)

c# - Why doesn't this goto inside this switch work?

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.

question from:https://stackoverflow.com/questions/23604677/why-doesnt-this-goto-inside-this-switch-work

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...