• 2 Posts
  • 13 Comments
Joined 1 year ago
cake
Cake day: June 17th, 2023

help-circle





  • A switch statement will let control fall through.

    I think even switch statement doesn’t allow it because every case needs to be terminated with either break, return or raise. One needs to use case goto if one wants fall thought like behavior:

    switch (x)
    {
    case "One":
        Console.WriteLine("One");
        goto case "Two";
    case "Two":
        Console.WriteLine("One or two");
        break;
    }
    

    C# outlaws this, because the vast majority of case sections do not fall through, and when they do in languages that allow it, it’s often a mistake caused by the developer forgetting to write a break statement (or some other statement to break out of the switch). Accidental fall-through is likely to produce unwanted behavior, so C# requires more than the mere omission of a break: if you want fall-through, you must ask for it explicitly.

    Programming C# 10 ~ Ian Griffiths