What this prompt does
This prompt refactors C# code to modern pattern matching available in [csharp_version]. It replaces [legacy_code_type] with switch expressions across [switch_scenarios], converts nested if-else property checks into property patterns for [property_scenario], swaps is/as casting chains for type patterns with when guards in [type_scenario], applies list patterns to [collection_type], uses relational patterns for [range_scenario], and combines and/or/not logical patterns for [complex_condition].
The structure works because tangled if-else and casting chains turn into readable switch expressions without changing behavior, which is exactly what the before/after examples verify. It produces [example_count] paired examples so you can see the transformation directly, and it closes with a BenchmarkDotNet comparison plus a note on whether pattern matching compiles to the same IL. That last step keeps a team confident the refactor preserves both behavior and performance rather than quietly altering either.
When to use it
- You are modernizing older C# services and want readable switch expressions in place of
[legacy_code_type] - Nested if-else property checks in
[property_scenario]have become hard to follow - is/as casting chains in
[type_scenario]could be cleaner type patterns with when guards - You are validating or deconstructing
[collection_type]and want list and positional patterns - Numeric validation like
[range_scenario]could use relational patterns instead of chained comparisons - You want before/after examples and a benchmark to prove the refactor changed nothing but readability
Example output
Expect paired before/after snippets: legacy switch statements and if-else chains rewritten as switch expressions, property-pattern refactors of nested conditionals, type-pattern replacements for casting chains, list-pattern examples deconstructing collections, relational and logical pattern combinations, a recursive-pattern example for nested object matching, and a BenchmarkDotNet comparison with commentary on whether the IL and performance are equivalent.
Pro tips
- Set
[csharp_version]accurately, since list patterns and other features require a specific language version and the model will otherwise suggest syntax you can't compile - Give real
[switch_scenarios]like HTTP status handling or an order state machine, so the examples map to code you actually maintain - Lean on when guards in
[type_scenario]to express conditions that a plain type pattern can't, but keep them readable rather than cramming logic in - Use relational patterns for
[range_scenario]like tax brackets —> 50000 and <= 100000reads far better than chained comparisons - Run the BenchmarkDotNet step; pattern matching usually compiles to comparable IL, but verifying it keeps the refactor honest for performance-sensitive paths
- Refactor behavior-preserving sections under test coverage, since the value here is cleaner code with identical results, not new functionality
- Combine logical patterns with
and,or, andnotfor[complex_condition]to express a single readable arm instead of several stacked if checks - Keep switch expressions exhaustive or add a discard arm, since an unhandled value otherwise throws at runtime rather than failing visibly during review