code-modernization

Refactors existing C# and VB.NET code to C# 14 and .NET 10 idioms.

1.2k|337|Updated Oct 13, 2022
One-click install
npx skills add https://github.com/dotnet/dotnet --skill code-modernization
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: code-modernization
Source: https://github.com/dotnet/dotnet/tree/main/src/winforms/.github/skills/code-modernization
Command: npx skills add https://github.com/dotnet/dotnet --skill code-modernization

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Legacy C# and VB.NET codebases accumulate outdated syntax, inconsistent null handling, and verbose patterns that increase maintenance cost. This Skill provides a rule-based checklist for safely modernizing existing source files to C# 14 / .NET 10 idioms without changing runtime semantics.

Core Features & Use Cases

  • Language Modernization: Converts block-scoped namespaces to file-scoped, replaces == null with is null, applies pattern matching, switch expressions, collection expressions, target-typed new(), and the C# 14 field keyword.
  • Var and Type Policy: Enforces a prioritized rule set for when to use var versus explicit types, including casts, generic method calls, and tuple deconstruction.
  • Comment and XML Documentation Cleanup: Fixes spelling and grammar in comments, requires XML doc headers on all types including private nested classes, and standardizes formatting (Allman braces, CRLF, UTF-8 with BOM).
  • Use Case: When asked to refactor a WinForms source file, apply the checklist to modernize null checks with ArgumentNullException.ThrowIfNull, convert event handlers to nullable signatures, and wrap long dot-chains — while skipping multi-targeted files where modern syntax would break older frameworks.

Quick Start

Ask the AI to modernize and refactor a specific existing C# file in this repository following the code-modernization rules.

Frequently Asked Questions about code-modernization

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I modernize legacy C# code to C# 14?

Apply safe transformations such as file-scoped namespaces, `is null` pattern matching, switch expressions, collection expressions, target-typed `new()`, and the `field` keyword. Only make changes that cannot alter runtime semantics, and verify the project's target framework first.

When should I use var versus explicit types in C#?

Never use `var` for primitive types like int or string. Use `var` when the type is visible on the same line, such as casts, generic method calls with explicit type arguments, or methods whose name implies the return type. Spell out the type when context does not reveal it.

Can I apply nullable reference types to .NET Framework projects?

Only if the whole project already processes nullable annotations or the file has an explicit `#nullable enable` directive. Do not add `#nullable enable` to files that lack it, since incremental annotation in a non-NRT-aware codebase risks introducing nullability bugs.

Why replace == null with is null in C#?

The `is null` pattern-matching form cannot be overridden by custom `==` or `!=` operators, making null checks reliable and consistent. It is the preferred idiom in modern C# codebases and avoids subtle bugs from overloaded equality operators.

What are the limitations of automated C# refactoring?

Transformations must never change runtime semantics; for example, `public Foo Bar => new Foo()` creates an instance per access while a property initializer creates one. Multi-targeted files and code under eng/common/ must be left untouched, and uncertain changes should get a TODO comment instead.