Earn More by Sharing What You Love
Do you create content for developers working with .NET, C#, Java, Python, or Node.js? Turn your expertise into extra income!

Gerald Versluis
9m 34s
In this article, we'll walk through the process of formatting your C# .NET code programmatically using the .NET format tool. This guide is based on Gerald Versluis's video tutorial on "Format Your C# .NET Code Automatically with .NET Format", where he demonstrates how to use the tool effectively to ensure your code adheres to consistent formatting standards. I'll provide timestamps and detailed explanations of what Gerald did in the video to help you follow along and apply these techniques to your projects.
At (0:36), Gerald introduces .NET format as a tool designed to format C# code based on the rules defined in an .editorconfig file. This file is pivotal in enforcing consistent coding styles throughout your project. It ensures that all code adheres to the same formatting guidelines, which can include:
By applying these rules, .NET format helps maintain a uniform codebase, making it easier to read and manage.
The .editorconfig file, as explained by Gerald at (0:48), specifies various coding style preferences. This file is highly flexible and allows you to define a wide range of formatting rules, including:
The .editorconfig file serves as a central point of configuration for code formatting across different editors and IDEs.
Gerald explains that .NET format is a global tool for the .NET runtime at (1:13). Being a global tool means it can be installed once and used across various .NET projects on your system. This allows for consistent code formatting without the need for separate installations or configurations for each project.
You can access the .NET format repository on GitHub at (1:26). This repository provides the source code, documentation, and additional information about the tool, helping users understand its functionalities and stay updated with the latest versions.
At (2:09), Gerald demonstrates the basic command for using .NET format:
dotnet format <options> <workspace>
In the context of .NET format, the term "workspace" refers to the scope of code that will be formatted. This can include:
The flexibility to specify different types of workspaces allows you to apply formatting across various levels of your project.
Gerald highlights several key features and options of .NET format at (2:27):
Gerald explores advanced options and customization. He mentions:
Gerald starts by explaining the installation process for the .NET format tool, which is essential for programmatically formatting C# code. He shows how to set it up as a global tool with the following command:
dotnet tool install -g dotnet-format
This command ensures that .NET format is available globally, allowing easy access from any command prompt or terminal. Gerald emphasizes the need for the .NET Core runtime, which should be installed if you've been working with .NET recently.

To confirm that the installation was successful, at (4:00), Gerald runs:
dotnet format /?
This command displays the list of available options and commands, verifying that .NET format is correctly installed and ready for use.

At (4:11), Gerald notes that some options, such as --dry-run, are deprecated and advises using updated practices for code formatting.
Using the Xamarin Community Toolkit codebase as a practical example, Gerald first checks for formatting issues without applying any changes. He runs:
dotnet format . -f --check
to identify files that need formatting. Gerald at (5:26) explains how you can specify the workspace using --folder for directories or .NET format sln for solution files. This command lists files with formatting issues.
Each process that executes has an exit code indicating the status. Gerald executed the following command to check the error level:
echo %errorlevel%
Gerald demonstrates that an exit code of 2 signals that there are formatting errors that need addressing.

Gerald then shows how to apply formatting fixes by running the command again, but without the --check flag:
dotnet format . -f
This will automatically format the code files based on the rules defined in your .editorconfig file. Gerald checks the exit code again using the same command as mentioned above, to ensure it is 0, indicating that all formatting issues have been resolved.
To verify the changes, at (8:00), he uses a graphical tool like GitHub Desktop to review the updated files. The tool shows improvements such as corrected whitespace, organized using statements, and other formatting adjustments.

Gerald recommends incorporating the .NET format tool into your build CI pipeline. This practice ensures that code formatting is consistently applied and helps maintain high-quality code standards. By automating the formatting process, you can avoid manual formatting tasks and ensure that all code adheres to the defined style rules.
By following Gerald's detailed walkthrough, you can easily integrate .NET format into your development process to automate the formatting of your C# .NET code. Whether you're working solo or as part of a team, this tool helps ensure that your code remains clean, consistent, and easy to read. Be sure to check out Gerald's video for a hands-on demonstration and further insights into the .NET format tool. Also do check out his YouTube Channel for more insights on C# code.
Do you create content for developers working with .NET, C#, Java, Python, or Node.js? Turn your expertise into extra income!
Join our newsletter, you’ll get exclusive access on article updates. We value your privacy