Large switch statements are not always a problem. They become a problem when the same set of cases is repeated across services, controllers, validators, formatters, and business logic.
A common example is an application that processes different payment methods:
switch (payment.Method)
{
case PaymentMethod.Card:
// Card logic
break;
case PaymentMethod.BankTransfer:
// Bank transfer logic
break;
case PaymentMethod.Wallet:
// Wallet logic
break;
case PaymentMethod.Cash:
// Cash logic
break;
}This looks manageable at first. The problem appears when each case grows or when the same switch needs to be updated in several places.
C# 15 introduces language features around closed hierarchies and union-style modeling that make this kind of code easier to structure. The goal is not to eliminate every switch, but to move behavior closer to the types that represent it and make the set of valid alternatives explicit.
What Is a Closed Hierarchy?
A closed hierarchy describes a type with a known set of permitted alternatives.
Conceptually:
Payment
|
+-- CardPayment
+-- BankTransferPayment
+-- WalletPayment
+-- CashPaymentThe important property is that the application knows the complete set of supported payment types.
This is different from an open inheritance hierarchy where any assembly can introduce another implementation:
public abstract class Payment
{
}With an open design, another developer can potentially create:
public sealed class CryptocurrencyPayment : Payment
{
}A closed hierarchy is useful when the business domain has a deliberately limited set of alternatives.
The Problem With Large Switch Statements
Consider an order processor:
public decimal CalculateFee(Payment payment)
{
switch (payment.Method)
{
case PaymentMethod.Card:
return payment.Amount * 0.02m;
case PaymentMethod.BankTransfer:
return 10m;
case PaymentMethod.Wallet:
return payment.Amount * 0.01m;
case PaymentMethod.Cash:
return 0m;
default:
throw new ArgumentOutOfRangeException();
}
}Now imagine another operation:
public string GetDisplayName(Payment payment)
{
switch (payment.Method)
{
case PaymentMethod.Card:
return "Card";
case PaymentMethod.BankTransfer:
return "Bank Transfer";
case PaymentMethod.Wallet:
return "Wallet";
case PaymentMethod.Cash:
return "Cash";
default:
throw new ArgumentOutOfRangeException();
}
}And another:
public bool RequiresAuthorization(Payment payment)
{
switch (payment.Method)
{
// More cases
}
}The same business knowledge is now duplicated.
Adding a new payment method means finding every switch and remembering to update it.
Replace the Enum With Domain Types
Instead of representing the payment method as an enum, create explicit domain types.
public abstract record Payment;
public sealed record CardPayment(
decimal Amount) : Payment;
public sealed record BankTransferPayment(
decimal Amount) : Payment;
public sealed record WalletPayment(
decimal Amount) : Payment;
public sealed record CashPayment(
decimal Amount) : Payment;Now the payment itself contains information about what it represents.
The application no longer needs a separate Method property to identify the type.
Move Behavior Into the Hierarchy
The next step is deciding whether the behavior belongs with the type.
For example:
public abstract record Payment
{
public abstract decimal CalculateFee();
}
public sealed record CardPayment(
decimal Amount) : Payment
{
public override decimal CalculateFee()
=> Amount * 0.02m;
}
public sealed record BankTransferPayment(
decimal Amount) : Payment
{
public override decimal CalculateFee()
=> 10m;
}
public sealed record WalletPayment(
decimal Amount) : Payment
{
public override decimal CalculateFee()
=> Amount * 0.01m;
}
public sealed record CashPayment(
decimal Amount) : Payment
{
public override decimal CalculateFee()
=> 0m;
}The caller becomes much simpler:
var fee = payment.CalculateFee();There is no switch in the calling code.
The behavior is associated with the type that owns the relevant business rule.
When a Switch Is Still Better
This does not mean every switch should be replaced with polymorphism.
Suppose the application needs to generate a report:
Payment
|
+-- Card
+-- Bank Transfer
+-- Wallet
+-- CashThe reporting requirement may belong to the reporting layer rather than the payment domain object.
In that situation, pattern matching can be appropriate:
return payment switch
{
CardPayment card =>
$"Card: {card.Amount:C}",
BankTransferPayment transfer =>
$"Bank transfer: {transfer.Amount:C}",
WalletPayment wallet =>
$"Wallet: {wallet.Amount:C}",
CashPayment cash =>
$"Cash: {cash.Amount:C}",
_ => throw new ArgumentOutOfRangeException()
};The problem is not the switch itself.
The problem is a switch that becomes the central location for every behavior in the system.
Closed Hierarchies Improve Domain Modeling
Consider an order state.
A typical implementation might use:
public enum OrderStatus
{
Pending,
Confirmed,
Shipped,
Delivered,
Cancelled
}This is simple, but the enum does not carry state-specific information.
A closed hierarchy can model richer states:
OrderState
|
+-- Pending
+-- Confirmed
+-- Shipped
+-- Delivered
+-- CancelledFor example:
public abstract record OrderState;
public sealed record Pending : OrderState;
public sealed record Confirmed(DateTime ConfirmedAt)
: OrderState;
public sealed record Shipped(
string TrackingNumber) : OrderState;
public sealed record Delivered(
DateTime DeliveredAt) : OrderState;
public sealed record Cancelled(
string Reason) : OrderState;Now each state can contain only the information relevant to it.
A shipped order has a tracking number.
A cancelled order has a cancellation reason.
A pending order does not need either.
Closed Types and Exhaustive Handling
One of the biggest advantages of a closed hierarchy is that the compiler and the type system can help developers reason about all valid cases.
For example:
static string Describe(OrderState state)
{
return state switch
{
Pending => "Order is waiting for confirmation",
Confirmed confirmed =>
$"Confirmed at {confirmed.ConfirmedAt}",
Shipped shipped =>
$"Tracking: {shipped.TrackingNumber}",
Delivered delivered =>
$"Delivered at {delivered.DeliveredAt}",
Cancelled cancelled =>
$"Cancelled: {cancelled.Reason}"
};
}If a new state is added, consumers that assume the existing set of cases may need to be reviewed.
This is one of the main benefits of modeling a finite domain explicitly.
Avoid the Default Case When Possible
A common pattern is:
return state switch
{
Pending => "...",
Confirmed => "...",
Shipped => "...",
Delivered => "...",
Cancelled => "...",
_ => throw new InvalidOperationException()
};The default case can hide missing cases.
If the hierarchy is intentionally closed and the compiler can prove that the alternatives are handled, an exhaustive switch without a catch-all can provide stronger feedback when the model changes.
This is particularly useful in domain code where silently ignoring a new state could create a business bug.
Closed Hierarchies for Command Handling
Another useful example is application commands.
Suppose an order system accepts several commands:
Join the conversation! Your thoughts help the community grow.