Blazor switch statement

A switch statement is used to test several conditions and executing a block of codes based on a specific condition.

@page "/"

@switch (itemIndex)
{
    case 0:
        <p>Do Item 1</p>
        break;
    case 1:
        <p>Do Item 2</p>
        break;
    case 2:
        <p>Do Item 3</p>
        break;
    default:
        <p>Do Nothing</p>
        break;
}

@functions {

int itemIndex = 1;

}

The switch statement with the condition of itemIndex equal to 1 produces the following:


<p>Do Item 2</p>

The break keyword exits from the switch statement while default keyword is a catch all if none of the switch conditions match.