A coding agent is only as good as the context you give it. Point Claude Code at a .NET solution with no guidance and it will write working C#, but in its own style, with its own folder layout and its own idea of what "done" means. Give it your conventions, a way to check its own work and a review step, and it starts to behave like a careful teammate. This guide sets that up for a .NET 10 solution.
What Claude Code is good at in a .NET codebase
Claude Code runs in your terminal (or the desktop app and IDE extensions), reads the repository, edits files and runs commands such as dotnet build and dotnet test. It works best on tasks with a clear finish line:
- Adding an endpoint, a service and its tests that follow an existing pattern
- Mechanical refactors across many files: renames, moving to primary constructors, nullable fixes
- Writing the first draft of EF Core migrations, options classes and validation
- Explaining unfamiliar code before you change it
It needs more care on design decisions with no right answer in the code: those are yours to make, and you tell it the answer.
Start with CLAUDE.md
CLAUDE.md at the repository root is read at the start of every session. Run /init once to generate a first version from the codebase, then cut it down to what the agent would otherwise get wrong. Short and specific beats long and general.
# Project: DevProgramming
## Build and test
- Build: `dotnet build -warnaserror`
- Test: `dotnet test DevProgramming.Tests --filter "Category!=E2E"`
- Never edit files under `Migrations/` by hand; add a migration with `dotnet ef migrations add <Name>`.
## Conventions
- .NET 10, C# 14, nullable enabled, file-scoped namespaces.
- Controllers stay thin: one service call per action. Business rules live in `Services/`.
- Every service method takes a `CancellationToken` as its last parameter.
- Read queries use `AsNoTracking()` and project to view models with `Select`.
- Options classes use `[Required]` and `ValidateOnStart()`.
## Do not
- Add NuGet packages without asking.
- Put secrets in `appsettings.json` examples; use placeholders.The "Do not" section earns its place: each line there is a mistake you saw once and never want to see again. Put rules for one folder in a CLAUDE.md inside that folder; it is loaded when the agent works there.
Let it check its own work with hooks
A rule in CLAUDE.md is a request. A hook is a guarantee: the harness runs it, not the model. Hooks live in .claude/settings.json and run shell commands on events such as PostToolUse (after a tool call) or Stop (when the agent finishes a turn).
This setup formats every C# file the agent edits and builds the solution when it finishes:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{ "type": "command", "command": "dotnet format whitespace -v q" }
]
}
],
"Stop": [
{
"hooks": [
{ "type": "command", "command": "dotnet build -warnaserror -nologo -v q 1>&2 || exit 2" }
]
}
]
}
}Exit code 2 is what makes this work: it tells Claude Code to block the stop and send the hook's error output back to the agent. Sending the build output to stderr (1>&2) means the compiler errors reach it, and it fixes them before handing over. You get code that compiles every time, without asking for it.
Keep hooks fast. A full test run on every edit slows everything down; run the unit tests in the Stop hook and leave the slow end-to-end tests for CI.
Permissions: allow the boring commands
Every command the agent runs asks for approval until you allow it. Allow the safe, read-only and build commands in the project settings so you only see prompts for the ones that matter:
{
"permissions": {
"allow": [
"Bash(dotnet build:*)",
"Bash(dotnet test:*)",
"Bash(dotnet format:*)",
"Bash(git status)",
"Bash(git diff:*)"
],
"deny": [
"Bash(dotnet ef database drop:*)",
"Read(./appsettings.json)"
]
}
}The deny list matters as much as the allow list. Keeping the agent away from the file that holds your real connection strings is a two-line change.
A task loop that works
- Explore first. Ask it to read the relevant code and explain the plan before editing. Plan mode does this without touching files.
- One task, one finish line. "Add
GET /api/posts/{slug}/relatedreturning at most 3 posts, with a test" beats "improve the posts API". - Point at an example. "Follow the pattern in
TagsControllerandTagService" saves a page of explanation. - Review the diff, not the summary. Read what changed as you would a pull request from a new teammate.
- Commit in small steps so a wrong turn costs one
git restore, not an afternoon.
Use it as a reviewer too
Before you open a pull request, ask Claude Code to review the branch against main, looking for bugs rather than style. Models are good at spotting the missing await, the query inside a loop and the CancellationToken that was accepted and never passed on. Treat its findings as a checklist to verify, not a verdict.
Wrapping up
Write down your conventions in CLAUDE.md, turn the ones that must always hold into hooks, allow the commands you trust and deny the ones you don't. Then give it small tasks with a clear finish line and review the diff. The agent writes faster than you do; the setup makes sure it writes your code, not its own.