C # Switch Performance

We use the switch statement to do some object processing based on a combination of conditions, with the default case, which we expect to call for all cases.

We have disagreements about the best way to approach this.

Some of us prefer Example A:

switch (task)
{
    case A:
        ProcessA();
        goto default;
    case B:
        ProcessB();
        goto default;
    case C:
        ProcessC();
        goto default;
    default:
        Final();
}

While others suggested it is better to use something like Example B:

switch (task)
{
    case A:
        ProcessA();
        break;
    case B:
        ProcessB();
        break;
    case C:
        ProcessC();
        break;
}

Final();

Since it Final()will be called in all cases.

This is a case of personal preference or there are objective differences in performance.

Are there any recommendations or corrections that we should pay attention to?

This is written in C # for the API and will be called very often. We strive to do everything right!

Hooray!

+4
2

, B.

"Jump". , . . B , .

, , (?). -, . , , .

+8

B - .

goto. . . goto , !

. Final() , .

-2

Source: https://habr.com/ru/post/1672456/


All Articles