How to make Visual Studio not put { on a new line?

C#.NetVisual StudioFormattingIf Statement

C# Problem Overview


If I have code like this

if(true){

and I add

}

it is transformed into

if (true)
{
}

but I would like it to stay in format

if (true) {
}

Even if I copy code that has if like this, it is transformed into the longer version.

P.S. I understand that it's a C++ and C# standard to use the longer version, however I do come from a different standard, and it's easier for me to use the shorter version.

I bet there is some formatter option that can be turned off or changed.

C# Solutions


Solution 1 - C#

Go to Tools -> Options -> Text Editor -> C# -> Code Style -> Formatting-> New Lines

Here there are a lot of options. Remove the check on all options here to never put the open bracket on a new line.

EDIT

The section New Line Options for expressions does not relate to placement of code in conjunction with brackets, so those you don't need to touch.

Solution 2 - C#

Bear in mind that if you're using Resharper the above procedure won't help, since Resharper overrides the native VS behavior. If that's your case go to Resharper > Options, Code Editing > C# > Formatting Style > Braces Layout, set all top "Braces Layout" options to "At end of line (K&R style)".

Took me a while to figure this out, so I hope it helps someone.

Solution 3 - C#

For all the Mac Users out there, this is how I solved it:

Preferences -> Source Code -> Code Formatting -> C# source code -> C# Format -> Edit

There you can change it under the Category: New Lines

image example

Solution 4 - C#

You can change that in the options of your Visual Studio. Go to Tools -> Options -> Text Editor -> C# -> formatting -> new lines (or something similar, I only have the german version of visual studio). You can then change where you want to put the brackets into the next line and where you don't. Personally, I removed all the options, but if you want to have a new line e.g. in loops, there are many options to customize this.

Solution 5 - C#

Have a look at Tools -> Options then Text Editor -> C# -> Formatting -> New Lines

Solution 6 - C#

For C++, the only way I can avoid VC++ putting '{' on new line for namespaces is by unchecking "Automatically format block when I type a }" option:

enter image description here

Solution 7 - C#

To do the same thing for CSS you have to go to: Tools > Options > Text Editor > CSS > Advanced > Formatting > Automatic Formatting > Off

CSS Auto Formatting Off

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIAdapterView Question on Stackoverflow
Solution 1 - C#Øyvind BråthenView Answer on Stackoverflow
Solution 2 - C#MaDDoXView Answer on Stackoverflow
Solution 3 - C#Hacks00rView Answer on Stackoverflow
Solution 4 - C#SörenView Answer on Stackoverflow
Solution 5 - C#NekreshView Answer on Stackoverflow
Solution 6 - C#Shital ShahView Answer on Stackoverflow
Solution 7 - C#AndrewView Answer on Stackoverflow