skip to Main Content

I use TABS as the whitespace character, CRLF as the line break thing, and I position my curly braces like this:

private bool Example() {
    return true;
}

So, not like this:

private bool Example()
{
    return false;
}

I have my VS 2022 C# text editor settings set to be the same way. Still, somehow whenever I use autocompleted code, for example ctor + [tab], or when Visual Studio adjusts namespaces in a file, it always uses the exact opposite of what my setting are. It is really annoying and I don’t want to have to fix it manually every time…

I’ve scoured the settings in Visual Studio, but I didn’t find anything else that could affect these things. How could I change the autoformatting to use the same settings as the C# text editor? Thanks.

2

Answers


  1. I have made some additions based on the information provided by user246821. Here is a tested approach which can come into force.

    1. Click "Tools → Options → Text Editor → C# → Code Style → General → New Lines → Generate .editorconfig file from settings" to export .editorconfig file, take care to ensure the file named as ".editorconfig" and keep it in the same directory as the .sln file.
    2. Open the ".editorconfig" file which is just generated, find the statement "csharp_new_line_before_open_brace = all"(value is just for example) and modify it to "csharp_new_line_before_open_brace = none"
    3. You can perform the clean and rebuild operation of the solution or reboot the Visual Studio to ensure the changes take effect.
    Login or Signup to reply.
  2. You may try creating a .editorConfig file as described in Code-style rule options and Code style preferences.

    In Visual Studio, change your settings as desired.

    For example, if one would like to have the following code style:

    public class Class1 
    {
        public Class1() {
            
        }
    }
    

    Set desired settings:

    In the Visual Studio menu:

    • Click Tools
    • Select Options…
    • Expand Text Editor
    • Expand C#
    • Expand Code Style
    • Expand Formatting
    • Select New Lines
    • Uncheck Place open brace on new line for methods and local functions
    • Click OK (This will close the Tools menu, but it’s necessary in order for the settings to be saved properly before generating the .editorconfig file)

    Generate .editorconfig:

    In the Visual Studio menu:

    • Click Tools
    • Select Options…
    • Expand Text Editor
    • Expand C#
    • Expand Code Style
    • Select General
    • Click enter image description here
    • Select desired location (ie: folder) to save to.
    • Click Save. If the .editorconfig file already exists, one will see a prompt, that states: .editorconfig already exists. Do you want to replace it? Click Yes.
    • Click OK

    Note: If one would like the .editorconfig file to apply to all of one’s projects, ensure that you save it to the parent directory that contains all of your projects. For example, if one stores all project/solutions in %UserProfile%DocumentsProjects, then save the .editorconfig in %UserProfile%DocumentsProjects.

    enter image description here

    For more information see EditorConfig File hierarchy and precedence

    According to Define consistent coding styles with EditorConfig:

    After you edit your EditorConfig file, you must reload your code files
    for the new settings to take effect.

    Alternatively, one may follow the steps described in Define consistent coding styles with EditorConfig to create the .editorconfig file.


    Test the changes:

    • Create a new Windows Forms App (.NET Framework) in Visual Studio (name: WindowsFormsApp1)

    • Add a class to the project (name: Class1.cs)

    • In Class1.cs, position the curor inside the curly braces for Class1, then type ctor. Next press the Tab key.


    Resources:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search