Hero image!

Format C# code with .NET

August 09, 2023 - Drygast
Best Practice C# Visual Studio

The popularity of .NET Framework has dramatically increased in recent years. Due to its versatile nature, .NET Framework is widely used nowadays in large enterprise applications and even mobile and web applications. To ensure the consistent coding style and high code quality, developers typically utilize static code analysis tools such as StyleCop and FxCop during the development. However, these tools use the old-style format for their rules/configuration which can be very tedious to maintain. As a result, the .NET Format tool has recently been introduced, which allows developers to define their coding style using the modern source code format.

dotnet format

The .NET Format tool is an open source command line tool developed by Microsoft. It is a Microsoft Visual Studio extension which supports a wide range of languages such as C#, VB.NET, F#, and C++/CLI. The primary purpose of .NET Format is to automatically format source code according to the user-defined coding style, so that the code is consistent and easy to read. It does this by reformatting the source code into a uniform and consistent codebase, instead of relying on manually entered style information.

The main difference between .NET Format and StyleCop lies in the source code format. With .NET Format, developers are able to define their own source code format via an editorconfig file. The editorconfig format consists of a series of rules which can be used to control the source code formatting. For example, developers can specify the indent size, whether tabs or spaces should be used, or if statements should be terminated with semicolons. Additionally, the editorconfig files can be used to specify additional language-specific rules such as C# access modifiers or C++ namespace declarations.

Another advantage of .NET Format is its improved support for Visual Studio. This allows developers to quickly and easily change the source code formatting without having to manually modify their editorconfig file. In addition, the .NET Format tool is integrated with Visual Studio's IntelliSense feature, so developers can quickly and easily change their source code formatting as they type.

To use the .NET Format tool, developers first need to define their coding style using an .editorconfig file. They can then invoke the .NET Format tool from the command line, or from within Visual Studio. The .NET Format tool will reformat the source code according to the rules specified in the editorconfig file, ensuring that it is consistent and easy to read.

Overall, the .NET Format tool offers a significant improvement over the old stylecop. By allowing developers to define their own source code format using editorconfig files, they can ensure that their code is consistent, readable, and visually appealing. Additionally, the .NET Format tool is fully integrated with Visual Studio, making it easier to use and more efficient to update coding style. Thus, .NET Format is a great tool for developers who want to ensure that their source code meets all standards of style and best practices.

Example

First - we need a project to test with. In Visual Studio, create a new Console App project. We will end up with a file containing this basic code:

namespace DotnetFormatTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}

Next, we add a editorconfig file.

Add editorconfig file

For some reason, this did not work when I tried it so I added an empty textfile named ".editorconfig" manually instead.

Add editorconfig manually as an empty textfile

I then added the file to the solution by rightclicking the solution and choosing add / existing item.

Add existing item

When this new empty file is opened up the first time inside Visual Studio, it is populated with a bunch of default data.

Default editorconfig according to Visual Studio

One thing that I want to do is to have an extra newline at the end of each file. It's just something that I've done since forever and I cannot stop doing. So I checked the box for the "Insert final newline" and saved the file. (insert_final_newline = true is added to .editorconfig)

Insert final newline

To test the changes, run dotnet format in a terminal.

dotnet format doing the thing

Run dotnet format automatically

To make sure that the formatting is done automatically, we could add the dotnet format call automatically every time the project is built. To do this, simply open up the project properties and navigate to the build tab.

Add dotnet format to the post-build textbox and change the "When to run the post build event" to Always.

Project properties - build tab

Next - build the project and you should see an extra empty line at the end of the the Program.cs file.

Extra line added at the end of the file

And that's it - pretty simple but also powerful.