Reputation: 3190
I am using a goto
inside of a switch statement to emulate fall through behavior.
My switch statement is using an enum
to determine which case to use.
When the case that contains the goto
is executed, the execution hangs at that point and the hosting process starts to use a lot more cpu.
My code looks something like the following:
switch (myEnum)
{
case HostClass.EnumType.var1: goto case HostClass.EnumType.var2;
case HostClass.EnumType.var2: myint = 3; break;
default: break;
}
Why does my switch statement hang on a goto? Reading online, this seems to be a common pattern.
Removing the goto fixes it, but I dont understand why.
Upvotes: 1
Views: 409
Reputation: 2830
You don't need the goto
. Just do this:
switch (myEnum)
{
case HostClass.EnumType.var1:
case HostClass.EnumType.var2:
myint = 3; break;
default: break;
}
Update
OK, so the reason (I believe) it doesn't work is because you need a place to goto
. For example:
for(var i = 0; i < 100; i+=1)
{
if(i == 50)
{
goto Outer;
}
}
Outer:
Console.WriteLine("Done");
This is a very contrived example.
I don't think this explains why your code hangs. The only thing I can think of is that the goto
is waiting for an address?
As mentioned in the comments by NeilP, You can only fall through on empty case-statements. This still doesn't explain why your code hangs on 'goto'. It shouldn't.
Upvotes: 9