Question
Can multiple values be handled by one branch of a C# switch statement without repeating the case keyword for every value?
For example, this works:
switch (value)
{
case 1:
case 2:
case 3:
// Do some work.
break;
case 4:
case 5:
case 6:
// Do different work.
break;
default:
// Handle every other value.
break;
}
Is there syntax such as case 1, 2, 3: to group values on one line, or is that syntax from another language?
Short Answer
A C# switch chooses code based on a value. To make several values run the same code, place multiple case labels before one shared statement block. In modern C# versions, you can also use an or pattern such as case 1 or 2 or 3:. Comma-separated labels, such as case 1, 2, 3:, are not valid C# syntax.
Concept
A switch statement compares one input value against a set of case labels. When a label matches, C# starts executing the statements associated with that label.
Multiple labels can point to the same block of code:
case 1:
case 2:
case 3:
HandleSmallNumber();
break;
The first two labels contain no statements. They simply redirect matching values to the shared code under case 3. This is commonly called grouping case labels.
In classic C#, writing case 1, 2, 3: is invalid. A comma does not mean “or” in a case label.
Modern C# supports pattern matching, including the or pattern:
case 1 or 2 or 3:
HandleSmallNumber();
break;
Both forms express the same idea. The stacked-label form works in older C# versions and remains common in existing codebases.
Mental Model
Think of a switch as a building with numbered entrances.
case 1:,case 2:, andcase 3:are three entrances.- When any of those entrances is chosen, they all lead into the same room, where the shared code runs.
breakis the exit from the building: it prevents the program from continuing into another branch.
With stacked labels, the first labels are signs pointing to the same room. With case 1 or 2 or 3:, the signs are written on one line.
Syntax and Examples
The traditional, widely compatible C# syntax is to stack empty case labels:
switch (value)
{
case 1:
case 2:
case 3:
Console.WriteLine("A low value was selected.");
break;
case 4:
case 5:
case 6:
Console.WriteLine("A middle value was selected.");
break;
default:
Console.WriteLine("A different value was selected.");
break;
}
Each value from 1 through 3 uses the first block. Each value from 4 through 6 uses the second block.
In C# 9 or later, use an or pattern when it improves readability:
switch (value)
{
case 1 or 2 or 3:
Console.WriteLine("A low value was selected.");
break;
case 4 or 5 or 6:
Console.WriteLine("A middle value was selected.");
break;
default:
Console.WriteLine("A different value was selected.");
break;
}
The following is not valid C#:
case 1, 2, 3: // Invalid C# syntax
break;
Step by Step Execution
Consider this code:
int value = 2;
switch (value)
{
case 1:
case 2:
case 3:
Console.WriteLine("Small group");
break;
case 4:
case 5:
Console.WriteLine("Large group");
break;
default:
Console.WriteLine("Unknown group");
break;
}
Execution steps:
valuecontains2.- The
switchchecks its labels in order. case 1:does not match.case 2:matches.- Since
case 2:has no statements of its own, execution reaches the shared statements belowcase 3:. - The program prints
Small group. breakexits theswitch; cases4,5, anddefaultare not executed.
Real World Use Cases
Grouping cases is useful whenever several input values have the same meaning or action.
- HTTP status handling: handle several client-error status codes with the same response path.
- Menu commands: map several keyboard shortcuts to one command.
- User roles: allow
AdminandManagerto perform the same operation. - Game input: treat multiple keys as movement in the same direction category.
- File extensions: process
.jpg,.jpeg, and.pngas image uploads. - Import data: map multiple external status codes to one internal status.
Example using an enum:
enum UserRole
{
Guest,
Member,
Moderator,
Admin
}
switch (role)
{
case UserRole.Moderator:
case UserRole.Admin:
DeleteComment();
break;
default:
ShowPermissionDenied();
break;
}
Real Codebase Usage
In production code, grouped cases are most useful when the grouped values genuinely share one behavior. Keep the shared block small and move substantial logic into a clearly named method.
switch (statusCode)
{
case 401:
case 403:
LogAuthorizationFailure(statusCode);
return;
case 404:
LogMissingResource();
return;
case 500:
case 502:
case 503:
ScheduleRetry();
return;
default:
LogUnexpectedStatus(statusCode);
return;
}
Common project patterns include:
- Validation: group invalid input categories and return an error early.
- Error handling: group error codes that need the same recovery action.
- Configuration: map several aliases to a normalized setting.
- Enums: handle related enum values with a shared permission, UI, or workflow rule.
For simple value-to-result mappings, a switch expression can be more concise:
string category = value switch
{
1 or 2 or 3 => "Low",
4 or 5 or 6 => "Middle",
_ => "Other"
};
Common Mistakes
Using comma-separated values
This is invalid in C#:
case 1, 2, 3:
Console.WriteLine("Grouped");
break;
Use stacked labels, or use or patterns in C# 9 and later.
Forgetting to end a non-empty case
C# does not generally allow execution to continue from a case that contains statements into the next case:
switch (value)
{
case 1:
Console.WriteLine("One");
case 2: // Compile-time error
Console.WriteLine("Two");
break;
}
End each non-empty branch with break, return, throw, or another statement that transfers control.
case 1:
Console.WriteLine("One");
break;
Duplicating a case value
Every constant case value must be unique:
case 1:
case 1: // Compile-time error: duplicate case label
break;
Using default as if it always runs
default runs only when no earlier case matches. It is not code that runs after every switch branch.
Grouping values with different meanings
Do not group cases only because the code currently looks similar. If the values represent different business rules, separate branches may be clearer and safer when requirements change.
Comparisons
| Approach | Example | When to use it |
|---|---|---|
| Stacked case labels | case 1: then case 2: | Compatible with older C# versions; clear for simple groups. |
or pattern | case 1 or 2: | C# 9+; compact and expressive for a list of alternatives. |
if with ` | ` | |
case with when | case int n when n >= 1 && n <= 3: | Best for ranges or conditions that are not a fixed list. |
| Switch expression | value switch { 1 or 2 => "A", _ => "B" } | Best when selecting and returning one value. |
For consecutive numeric values, a range pattern can be clearer than listing each number:
switch (value)
{
case >= 1 and <= 3:
Console.WriteLine("Small group");
break;
case >= 4 and <= 6:
Console.WriteLine("Middle group");
break;
}
Range patterns require modern C# and are appropriate only when every value in the range should match.
Cheat Sheet
// Traditional grouped cases
case 1:
case 2:
case 3:
DoWork();
break;
// C# 9+ alternative
case 1 or 2 or 3:
DoWork();
break;
// Default branch
switch (value)
{
default:
HandleUnknownValue();
break;
}
// C# 9+ range pattern
case >= 1 and <= 3:
DoWork();
break;
Rules:
case 1, 2, 3:is not valid C#.- Several empty labels may share one statement block.
- A non-empty
casenormally must end withbreak,return,throw, or similar control transfer. defaultruns only if no case matches.- Do not repeat the same constant case label.
FAQ
Can I write case 1, 2, 3: in C#?
No. C# does not support comma-separated values in a case label. Use stacked case labels or, in C# 9+, case 1 or 2 or 3:.
Does C# allow switch case fall-through?
C# allows empty case labels to lead to one shared block. It does not allow a case with executable statements to silently continue into the next case.
Do I need break after every C# case?
A branch with statements must end by transferring control, often with break. A return or throw can also end the branch. Empty grouped labels do not need break.
What C# version supports case 1 or 2?
The or pattern was introduced in C# 9. Use stacked labels if your project targets an older language version.
Should I use grouped cases or an if statement?
Use a switch when one value is being compared with several discrete alternatives. Use if when conditions involve different variables, boolean logic, or more complex comparisons.
How can I match a numeric range in a switch?
In modern C#, use relational patterns such as case >= 1 and <= 3:. In older code, use a when condition or an if statement.
Can multiple enum values use the same switch branch?
Yes. Group enum labels exactly as you group numeric labels, such as case UserRole.Admin: followed by case UserRole.Moderator:.
Mini Project
Description
Build a small delivery-status formatter. A delivery service receives numeric status codes and must turn them into clear messages. Several status codes share the same message, making grouped case labels a practical fit.
Goal
Create a C# program that groups delivery status codes and prints a message for each group.
Requirements
Use an integer variable named statusCode.
Group status codes 1, 2, and 3 as processing statuses.
Group status codes 4 and 5 as completed statuses.
Handle all other codes with a default branch.
Ensure every non-empty branch ends correctly.
Keep learning
Related questions
AddTransient vs AddScoped vs AddSingleton in ASP.NET Core Dependency Injection
Learn the differences between AddTransient, AddScoped, and AddSingleton in ASP.NET Core DI with examples and practical usage.
Best Way to Repeat a Character in C#: Building Repeated Strings Efficiently
Learn the best way to repeat a character in C#, compare StringBuilder, string concatenation, and simpler built-in options.
C# Access Modifiers and static: public, private, protected, and Defaults
Learn how C# public, private, protected, and default access control visibility, and how static differs from instance members.