Use a 'goto' in a switch?

前端 未结 7 1367
别跟我提以往
别跟我提以往 2020-12-03 00:25

I\'ve seen a suggested coding standard that reads Never use goto unless in a switch statement fall-through.

I don\'t follow. What exactly would this \'

7条回答
  •  孤城傲影
    2020-12-03 00:57

    I know that is old topic but question is still actual. Could we use the next code instead ugly version with goto statement ?

    var variable = 2;
    switch (variable)
    {
    case 2:
    Console.WriteLine("variable is >= 2");
    goto case 1;
    case 1:
    Console.WriteLine("variable is >= 1");
    break;
    
    
    }
    

    might be substituted with the next neater code:

    if (variable >= 2)
    {
    Console.WriteLine("variable is >= 2");
    }
    if (variable >= 1)
    {
    Console.WriteLine("variable is >= 1");
    }
    

提交回复
热议问题