Prompt Engineering Refactoring C#

Modernize C# Code — Current Language, Identical Behavior

Pattern matching, records, nullable annotations, async done right — a C# modernization scope with the rule that matters: modern equivalents are rarely exact equivalents.

Overview

C# has changed enough that pre-C# 8 code reads like a different language — but modernization is a behavior minefield: LINQ changes evaluation laziness, async changes timing, records change equality semantics. This prompt sets a concrete C# modernization scope: pattern matching and switch expressions over conditional chains, records for immutable data carriers, nullable reference annotations where the project enables them, LINQ only where it stays readable, async/await over raw continuations and never blocking with .Result. The modernization goal's warning does the safety work: check null handling, error behavior, and lazy-vs-eager evaluation before swapping anything.

Workflow

  1. Scope the features

    A named list — patterns, records, nullable annotations, async hygiene — not a vague "make it modern".

  2. Swap with suspicion

    Each replacement is checked against documented behavior differences: null handling, laziness, equality.

  3. Verify edge cases per API

    Old and new calls must agree where it hurts — empty sequences, nulls, exceptions — not just the happy path.

Why This Works

  • A concrete feature scope produces consistent modernization instead of taste
  • The not-exact-equivalents warning targets where modernizations actually regress
  • Per-replacement verifiability keeps the diff reviewable

Best for

  • .NET codebases upgraded by framework but not by idiom
  • Teams adopting nullable reference types incrementally
  • Code written before records and switch expressions existed

Not for

  • Reviewing C# code quality without transforming it — that's the Code Review Prompt Generator
  • Optimizing the SQL inside the C# — that's the SQL Optimization Prompt's space

Use cases

  • Bringing a C# 7-era service class to current C#
  • Replacing conditional chains with pattern matching
  • Untangling .Result blocking from async call paths

Tip: Save time by exploring related resources and tools that integrate with this workflow.

Explore all resources