Skip to content

SwitchExpressionBuilder

Compose C# switch expressions with pattern arms and nested switches.

See also: BodyBuilder | SwitchStatementBuilder | Emit Overview


Factory Methods

Method Description
Empty() Create an empty switch expression builder

Arm Building

Method Description
Arm(string pattern, ExpressionRef expression) Add an arm with a pattern and result
Arm(string pattern, ExpressionRef nestedExpr, configureNested) Add an arm with a nested switch expression
Default(ExpressionRef expression) Add the discard (_) default arm

The pattern parameter accepts any C# pattern — type patterns, property patterns, list patterns, relational patterns, and when guards.

Example

// Simple type-pattern switch
body.AddSwitchExpression("@event", sw => sw
    .Arm("OrderCreated e", "HandleCreated(e)")
    .Arm("OrderShipped e", "HandleShipped(e)")
    .Default("Acknowledge()"))

// Nested switch expression
body.AddSwitchExpression("payload", sw => sw
    .Arm("BlockActionsPayload ba", "ba.Actions", nested => nested
        .Arm("""[{ ActionId: "approve" } action, ..]""", "HandleApprove(action)")
        .Default("Acknowledge()"))
    .Default("Acknowledge()"))

// Patterns with when guards
body.AddSwitchExpression("statusCode", sw => sw
    .Arm("> 500", "HandleServerError()")
    .Arm("> 400", "HandleClientError()")
    .Arm("200", "HandleSuccess()")
    .Default("HandleUnknown()"))

Properties

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