Home › .NET / ASP.NET Core › C# / .NET Copilot path instructions

C# / .NET Copilot path instructions

Path-specific Copilot instructions for C# files on .NET 10: nullable types, async, records, and logging.

GitHub Copilot.NET / ASP.NET Core

How to use

Save this as .github/instructions/csharp.instructions.md in your repository root, commit it, and your agent will pick it up automatically. Edit the commands to match your project.

.github/instructions/csharp.instructions.mdDownload  
---
applyTo: "**/*.cs"
description: C# conventions
---

# C# (.NET 10)

- Nullable reference types are enabled. Model optionality with `?` and handle it; don't use `!` to silence warnings.
- File-scoped namespaces, primary constructors for DI-only classes, `record` for immutable data, collection expressions (`[]`).
- Async I/O everywhere: `async Task`/`ValueTask`, accept and pass `CancellationToken`, never `.Result`/`.Wait()`/`async void` (except event handlers).
- Use `ILogger<T>` with message templates (`"Processing {OrderId}"`), not interpolated strings.
- Guard clauses with `ArgumentNullException.ThrowIfNull(x)` / `ArgumentException.ThrowIfNullOrEmpty(s)`.
- Prefer pattern matching (`is`, `switch` expressions) over casts and long if/else chains.
- LINQ for clarity, loops for hot paths; materialize EF queries once (`ToListAsync`).
- `IDisposable`/`IAsyncDisposable` resources in `using`/`await using`.
- Configuration via the options pattern with validation (`ValidateOnStart`).
- Public APIs get XML doc comments.

## Check
`dotnet build -warnaserror && dotnet test && dotnet format --verify-no-changes`.

Related rules

Angular 20 Copilot instructions

Copilot instructions for Angular 20 apps using standalone components, signals, and the new control flow.

GitHub CopilotAngular

Code review Copilot instructions

Copilot instructions focused on pull request review: what to flag, severity labels, and how to word comments.

GitHub CopilotAgent workflows

Dockerfile Copilot path instructions

Path-specific Copilot instructions for Dockerfiles and compose files: multi-stage builds, non-root, and pinned images.

GitHub CopilotDocker

Markdown docs Copilot path instructions

Path-specific Copilot instructions for Markdown docs and READMEs: structure, runnable commands, and concise style.

GitHub CopilotAgent workflows

Get new rules weekly

One email a week: fresh rule files, agent tips, and what's changing in Cursor, Claude Code and Copilot. Free, unsubscribe anytime.

Opens the Rulestack Newsletter on beehiiv in a new tab. Or subscribe directly on beehiiv.