DX - Code Formatting
- Andrew Boyd

- Aug 13
- 2 min read
I’m a big fan of the Developer Experience (DX). Too often, developers are told to “just do it”, and we’re not really given time to make the refactors we *know* would pay off.
If you’ve written any JavaScript, you’ve probably had the privilege of experiencing Prettier. This post is about getting as close as possible to that same experience in C#—reducing noise in your `.cs` files and making formatting consistent across a team.
1) Excessive namespaces
Remove unused usings
First up: unused namespaces.
Have you ever noticed a `using` is greyed out in your IDE? That normally means it isn’t used, so it can be removed. It’s a small change, but it adds up fast—especially in older codebases.
Enable implicit usings (.NET 6+)
There’s also a feature you can turn on in your `.csproj` (or `Directory.Build.props`) called:
<ImplicitUsings>enable</ImplicitUsings>This was introduced in .NET 6. Scott Hanselman covers what this gives you (and which namespaces you no longer need to explicitly include) here:
- Implicit usings in .NET 6: https://www.hanselman.com/blog/implicit-usings-in-net-6
The gotcha
This can break your build.
Once implicit usings are enabled, every C# file effectively gets common namespaces (like `System`) “for free”. That’s great—until you have types with the same name in different namespaces.
When that happens, you’ll need to disambiguate by:
- fully qualifying the type name, or
- using an alias.
2) Coding standards with `.editorconfig`
Most teams have coding standards, but did you know you can enforce them with a `.editorconfig` file?
There are a lot of options available, so going through them all is beyond this post, but Microsoft’s docs are a good starting point:
- Code style rule options: https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/code-style-rule-options
I also have a sample `.editorconfig` in my open-source project here:
- WiseAuth `.editorconfig`: https://github.com/andrewjboyd/WiseAuth/blob/main/.editorconfig
A couple of my favourites
File-scoped namespaces
Prefer:
namespace YourApp;over:
namespace YourApp
{
// ...
}Multi-line blocks for readability
Prefer:
if (someComparison)
{
return someResult;
}over:
if (someComparison) return someResult;3) Bringing it all together with `dotnet format`
As of .NET 6, Microsoft introduced `dotnet format`:
- `dotnet format` docs: https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-format
Once you’ve got an `.editorconfig` in place, this becomes the “make it all consistent” button.
At a high level, the workflow looks like:
Enable Implicit Usings (if you haven't already)
Commit the changes
Add/adjust `.editorconfig`
Commit the changes (yes, I like to commit regularly, but you'll thank me if the next step blows out)
Run: dotnet format
Commit the changes (ideally as a dedicated formatting PR. A word of warning: it will be huge because it will tidy up every cs file in your repo)
Final thoughts
None of this is glamorous work, but it’s the kind of thing that improves DX immediately:
- less noise in diffs
- fewer style debates in PRs
- more consistent code across the team
If you’ve ever wished C# had a “Prettier moment”, this is the closest I’ve found.
Visual Studio extension
There is also a feature in Visual Studio that allows you to format your files on save by Mads Kristensen called CodeCleanupOnSave ( https://marketplace.visualstudio.com/items?itemName=MadsKristensen.CodeCleanupOnSave).
Comments