Code style options and code cleanup - Visual Studio (Windows) (original) (raw)

Code style preferences allow you to control aspects of code such as indent style, tab width, end-of-line characters, encoding, and many other formatting choices. You can define code style settings in two ways:

You can also configure Visual Studio to apply code style preferences using the Code Cleanup and Format Document commands.

Code styles in EditorConfig files

Code style settings can be specified by adding an EditorConfig file to your project. EditorConfig files are associated with a codebase rather than a Visual Studio personalization account. Settings in an EditorConfig file take precedence over code styles that are specified in the Options dialog box. Use an EditorConfig file when you want to enforce coding styles for all contributors to your repo or project. They are especially useful to ensure consistency in a teams programming environment.

To add an EditorConfig file, see Add and remove EditorConfig files.

For reference information on .NET code style settings, see Code style settings.

C/C++ code styles in the Options dialog box

You can specify many individual code formatting options, such as indentation and brace positions. To do so, go to Tools > Options > Text Editor > C/C++ > Code Style > Formatting (or type Ctrl + Q and search for "Formatting"). Alternatively, you can specify one of the ClangFormat styles (or your own custom ClangFormat style).

Screenshot of the Options pane with Text Editor.

For more information about all the formatting options, see Options, Text Editor, C/C++, Formatting.

.NET code styles in the Options dialog box

Code style preferences can be set for all of your C# and Visual Basic projects by opening the Options dialog box from the Tools menu. In the Options dialog box, select Text Editor > [C# or Visual Basic] > Code Style.

Each item in the list shows a preview of the preference when you select the option:

Screenshot of code style options.

Options set in this window are applicable to your Visual Studio personalization account and aren't associated with a particular project or codebase. In addition, they aren't enforced at build time, including in continuous integration (CI) builds. If you want to associate code style preferences with your project and have the styles enforced during build, specify the preferences in an EditorConfig file that's associated with the project.

Preference and severity

For each code style setting on the General and Naming options pages, you can set the Preference and Severity values using the drop-downs on each line. Severity can be set to Refactoring Only, Suggestion, Warning, or Error.

Settings in an EditorConfig file take precedence over code styles set in these pages.

Enforce code styles on build

Starting in Visual Studio 2019 version 16.8, which includes the .NET 5.0 RC2 SDK, you can enforce the .NET coding conventions on build for all .NET projects. At build time, .NET code style violations will appear as warnings or errors with an "IDE" prefix. This enables you to strictly enforce consistent code styles in your codebase.

Apply code styles

When you change a code style in the Options page or add an EditorConfig file to your project in Visual Studio, only new lines of code are formatted based on the new settings. The formatting of existing code isn't changed unless you run one of the following commands:

Apply code styles using code cleanup

To apply code styles from an EditorConfig file or from the Code Style options page, use the Code Cleanup button at the bottom of the editor (keyboard: Ctrl+K, Ctrl+E). If an EditorConfig file exists for the project, those are the settings that take precedence.

Tip

.NET rules configured with a severity of Refactoring Only don't participate in code cleanup but can be individually applied via the Quick Actions and Refactorings menu.

To apply code styles:

  1. First, configure which code styles you want to apply (in one of two profiles) in the Configure Code Cleanup dialog box. To open this dialog box, click the expander arrow next to the code cleanup broom icon and then choose Configure Code Cleanup. Alternatively, use the Analyze > Code Cleanup menu.
    Screenshot of Configure Code Cleanup.
    For a mapping of .NET code cleanup fixers to .NET code style rules, see .NET code cleanup settings.
    C/C++ options include C++ in the name of the fixer.
  2. After you've configured code cleanup, use one of the following methods to run code cleanup:
    • Click on the broom icon or press Ctrl+K, Ctrl+E.
      Screenshot of Execute code cleanup.
    • To run code cleanup across your entire project or solution, right-click the project or solution name in Solution Explorer, select Analyze and Code Cleanup, and then select Run Code Cleanup.
      Screenshot of Run Code Cleanup across entire project or solution.
  3. (Optional) If you want your code style settings to be applied every time you save a file, go to Options > Text Editor > Code Cleanup and select Run Code Cleanup profile on save.