How to Make AI Write High-Quality Code in .NET A .NET developer describes how to make AI coding agents produce high-quality C# code by encoding standards in build-time enforcement rather than prompts. The approach centers on a Directory.Build.props file with settings such as TreatWarningsAsErrors, AnalysisMode All, and EnforceCodeStyleInBuild, so that an agent's dotnet build output surfaces violations as errors it must fix before a human reviews the diff. The developer argues prompt-based rules degrade as session context fills, while build-enforced rules are followed every time. AI writes .NET code faster than anyone on your team. It can also write code that looks good but can fall apart during careful review. I have spent years improving the quality of code I write, long before the AI was around. I learned to write code that is easy to read and maintain, and I have been refining my skills ever since. Now I'm doing the same by teaching AI agents to write code with the same quality I do. But most developers fail to get AI to write high-quality, clean code. The reason is simple. The AI followed the rules it could see, and it never saw yours. Most developers try to fix this by putting the rules in the prompt. I tried that for many months. The rules work for a few files, maybe a few iterations in a session. But as the session goes on, the context gets loaded with information, and your rules start to be ignored. So only two things actually work reliably that I want to show you today: In this post, we will explore: Let's dive in. 👉 Read original article on my newsletter: https://antondevtips.com/blog/how-to-make-ai-write-high-quality-code-in-dotnet https://antondevtips.com/blog/how-to-make-ai-write-high-quality-code-in-dotnet Every .NET solution should start with a Directory.Build.props file. This file defines project-wide settings that apply to all projects in your solution. Without this file, you end up duplicating the same configuration across multiple .csproj files. When you want to change a setting, you have to update every project file by hand. This leads to inconsistencies and wasted time. Directory.Build.props solves this problem by centralizing configuration in a single file. You create this file in the same directory as your .sln file, and MSBuild applies it to all projects in the solution. Here is the configuration I use for every new project: