Search code examples
c#visual-studiovisual-studio-2019

How to prevent Visual Studio from formatting specific code?


I type the code below:

if (rows == 0) break;

and when I add the semicolon at the end, VS springs into action and converts it to:

if (rows == 0) 
    break;

Same thing happens with if (rows = 0) return; and some other constructs.

How can I prevent VS from formatting this specific code interaction without messing with all the other niceties that it provides? I looked in Tools/Options/Text Editor/C#/Code Style/Formatting, but don't see anything obvious.


Solution

  • You can enable the VS option in main menu:

    Tools > Options > Text Editor > C# > Code style > Formatting > Wrapping >

    Leave statements and member declarations on the same line

    enter image description here

    English version of same options dialog (VS2017 version):

    enter image description here