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.

How to use this resource

  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

FAQ

Will modernizing to records or LINQ quietly change how my C# behaves?

The prompt is built to prevent that. Its known-failure-mode warning says "modern equivalents are rarely exact equivalents — check null handling, error behavior, lazy vs eager evaluation, and culture/locale defaults before swapping," and validation verifies each API replacement against documented edge cases. "If achieving the goal and preserving behavior conflict, behavior wins." It generates the refactor prompt; you run it and regression-test the diff yourself.

What if part of my C# can't be safely modernized from what I paste in?

It leaves that code alone and says why. The objective reads "If a transformation cannot be made safely with the available information, leave that code unchanged and say why," and skipped transformations land under "Suggested but not applied" with the information that would unlock them. Blocking on async with .Result, for instance, is only untangled when the call graph is clear. You review before applying.

More resources from Refactor Prompt Builder

Resources that pair well

Related tools

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