Skip to main content

Switch statements in U# 1.X

Switch statements get compiled to a jump table if
1. The input evaluates to an integer type (which enums do at runtime)
2. The switch has at least 4 value cases and no more than 257 value cases
3. The values in each case are in the range of `0 - 256` (inclusive)

This means you can have at most **257** explicit value cases and a `default` case.
The cases can be declared in any order.

Example of a switch statement that will be compiled to a jump table:
```cs
switch (inputValue)
{
    case 256:
        Debug.Log("256");
        break;
    case 0:
        Debug.Log("0");
        break;
    case 2:
        Debug.Log("2");
        break;
    case 123:
        Debug.Log("123");
        break;
    default:
        Debug.Log("Default value");
        break;
}
```

Example of a switch statement that does not get compiled to a jump table and instead becomes `if else if else if else ...`
```cs
switch (inputValue)
{
    case -1:
        Debug.Log("-1");
        break;
    case 257:
        Debug.Log("257");
        break;
    default:
        Debug.Log("Default value");
        break;
}
```