Skip to content

SwitchStatementBuilder

Compose C# switch statements with case sections and a default section.

See also: BodyBuilder | SwitchExpressionBuilder | Emit Overview


Factory Methods

Method Description
Empty() Create an empty switch statement builder

Case Building

Method Description
Case(string label, configure) Add a case section with a pattern label
Default(configure) Add the default section

The label parameter accepts any C# case pattern — constant values, type patterns, property patterns, etc. Each configure callback receives a BodyBuilder for the case body.

Case bodies that contain local declarations are automatically wrapped in a block scope.

Example

// String matching
body.AddSwitch("job.JobType", sw => sw
    .Case("\"create\"", cb => cb
        .AddStatement("HandleCreate()")
        .AddStatement("break"))
    .Case("\"update\"", cb => cb
        .AddStatement("HandleUpdate()")
        .AddStatement("break"))
    .Default(cb => cb
        .AddStatement("break")))

// Type pattern matching
body.AddSwitch("@event", sw => sw
    .Case("MyEvent e", cb => cb
        .AddStatement("await HandleEvent(e)")
        .AddStatement("break"))
    .Case("OtherEvent o", cb => cb
        .AddStatement("await HandleOther(o)")
        .AddStatement("break"))
    .Default(cb => cb
        .AddStatement("break")))

Properties

sw.IsEmpty  // bool — true if no sections added