dotnet/efcore · error · NotImplementedException
Empty switch statement
Error message
Empty switch statement
What it means
When a void switch is rewritten to conditionals (RewriteSwitchToConditionals), the aggregate over an empty case list with a null default body yields null, and the translator throws NotImplementedException("Empty switch statement"). A switch with zero arms and no default is degenerate.
Source
Thrown at src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs:2385
}
default:
throw new ArgumentOutOfRangeException();
}
static ConditionalExpression RewriteSwitchToConditionals(SwitchExpression node)
{
if (node.Type == typeof(void))
{
return (ConditionalExpression)(node.Cases
.SelectMany(c => c.TestValues, (c, tv) => new { c.Body, Label = tv })
.Reverse()
.Aggregate(
node.DefaultBody,
(expression, arm) => expression is null
? Expression.IfThen(Expression.Equal(node.SwitchValue, arm.Label), arm.Body)
: Expression.IfThenElse(Expression.Equal(node.SwitchValue, arm.Label), arm.Body, expression))
?? throw new NotImplementedException("Empty switch statement"));
}
Check.DebugAssert(node.DefaultBody is not null, "Switch expression with non-void return type but no default body");
return (ConditionalExpression)node.Cases
.SelectMany(c => c.TestValues, (c, tv) => new { c.Body, Label = tv })
.Reverse()
.Aggregate(
node.DefaultBody,
(expression, arm) => Expression.Condition(
Expression.Equal(node.SwitchValue, arm.Label),
arm.Body,
expression));
}
}
/// <inheritdoc />
protected override Expression VisitTry(TryExpression tryNode)View on GitHub (pinned to dbf9771522)
Solutions
- Add at least one case or a default body before the switch is translated.
- Remove the empty switch entirely — it has no effect.
- Validate that a switch has arms before constructing it.
Example fix
// before
var sw = Expression.Switch(value); // no cases, no default -> throws
// after
var sw = Expression.Switch(value, Expression.Empty(), new[] { case1 }); Defensive patterns
Strategy: validation
Validate before calling
protected override Expression VisitSwitch(SwitchExpression s) {
if (s.Cases.Count == 0 && s.DefaultBody is null) Found = true;
return s;
} Prevention
- Never construct a switch with zero arms and no default.
- Drop degenerate switches entirely — they are no-ops.
- Validate arm count before building a switch.
When it happens
Trigger: A SwitchExpression of void type constructed with no cases and a null default body, e.g. Expression.Switch(value).
Common situations: Programmatically constructed degenerate switches; expression rewriting that removes all arms leaving an empty switch.
Related errors
- Switch with non-null comparison method
- Missing default arm for switch expression
- Null argument in VisitLabelTarget
- Non-void label target
- DebugInfo nodes are not supporting when translating expressi
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/62887dc851dbd2f7.
Report an issue: GitHub.