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

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.

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.

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.

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

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.

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

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:

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

Related

How to get Visual Studio 2017 editor to support a different coding style?

The coding style that my employer uses states that the brace { should go on the same line, not the next one. Example:
class bar {
private void foo() {
if (1) {
Console.WriteLine("great");
} else {
Console.WriteLine("also great");
}
}
}
Visual Studio's default is to place braces on their own line.
How can I get Visual Studio to respect this coding style?
(I don't have 2017, but this is what I see in 2015, so it may be similar.)
Under Tools -> Options -> Text Editor -> C# -> Formatting -> New Lines, there are several options for "Place open brace on new line for <X>" (where X may be "types" or "methods", etc), as well as "Place <X> on new line", where X is "else" or "catch" or "members in anonymous types".
You can check and uncheck these boxes as required to match the style your company sets up.
In VS 2017 you can use the following Option; TextEditor -> C# -> Code Style -> Formating -> New Lines:
I believe it is called K&R style. I use JetBrains ReSharper, but it is not free.

Format document in Visual Studio to insert braces?

Is there a way to "Format document" in Visual Studio to insert braces around single-statement blocks for C# code? For example this:
if (x)
y();
... would become something like:
if (x) { y(); }
The auto formatting seems to deal with indentation but not this brace insertion. Is there a way to do it?
Actually, there seems to be something built-in to Visual Studio to do this.
If you go to Tools -> Options -> Text Editor -> C# -> Formatting -> New Lines and make sure that you have Place open braces on new lines for control block checked.
Then, go to your document and use the key combination CTRLKD, this should reformat your document and add the curly braces.
In case you have resharper you can configure it to force braces depending on your criteria.
Than in the existing code press ctrlaltshiftf, it will format whole file. Or select just part of the code, in this case resharper will format just selection
P.S. ctrlaltf opens clean up dialog. You can configure cleanup options.
I gave a vote to Gimly's answer as it is pretty much correct. These things change over time of course. I would have added a comment but I wanted to paste in some images. Location of settings in VS2019 is at:
Uncheck the appropriate check boxes and select OK.
The shortcut key did not work, which is a shame because, I love them! This documentation suggests that you use
CTRL-K, CTRL-E or, you use the broom icon:
.
Neither option worked for me perhaps because, that is not the intention, given the list of options. However, if you mark and cut all your code and then, paste the code back into the file, the new standard is adopted.

Break lines and wrapping in auto formatting of Visual Studio with ReSharper

I'm working in a C# project and using Visual Studio 2012. When Visual Studio tries to format my code, it breaks lines and make my code look difficult to read. The original code (what looks pretty nice to read for me and my team):
if (list.Any(x => x.Type == (int) EnumType.Customer))
{
}
And when Visual Studio tries to format:
if (
list.Any(
x => x.Type ==
(int) EnumType.Customer))
{
// Other break codes
}
There are a lot of other parts where it is breaking my code. I like auto formatting for some parts, but my question is: Is there a way to disable this break lines from auto formatting in Visual Studio?*
PS: I also have ReSharper installed.
Solution for long lines:
ReSharper, menu Options → Code Editing → C# → Formatting Style → Line Breaks And Wrapping.
And disable Wrap long lines:
And it really makes me crazy!
In ReSharper's settings, in the Languages section, you can change the formatting style of your C# code. The option you're looking to disable is something along the lines of "Indent anonymous method body." You can also look through the options to further customize the formatting style to your preference.

Making Resharper put a space in-between class/namespace identifier and the curly brace

I've just installed Resharper on my machine, and by default he presents me with the following C# code formatting:
namespace machineLearning{
public class Class1{
}
}
I've tried fiddling with the different options on Options -> C# -> Formatting Style but I can't seem to find what the option to correct this behaviour is. There seems to be no option explicitly or less-explicitly concerning adding a space between the identifier and the following brace.
How to accomplish that?
In Resharper 7 It is under Resharper -> Manage Options -> Code Editing -> C# -> Formatting Style -> Braces Layout -> Method declaration -> (set the value to ) At End of line (K & R Style)

Visual studio formatting issue

I am using Visual Studio 2008. In my editor whenever I write an automatic property it's "get" and "set" wraps to a single line whenever I wrote "set" and put semicolon(;). like this:
public string MyProperty
{
get; set;
}
I want it to be formatted like this
public string MyProperty
{
get;
set;
}
Currently I am manually formatting it to this after writing it. How can I set it as a default format?
Edit:
Options > Text Editor > C# > Formatting > Wrapping > Leave block on single line is already unchecked for me.
I unchecked all three option available in Options > Text Editor > C# > Formatting > General, but it doesn't work. Anything else?
If you put it all on one line, the default formatting options will leave it alone:
public string MyProperty { get; set; }
This is controlled by:
Options > Text Editor > C# > Formatting > Wrapping > Leave block on single line
If you really want to format it your way, you could disable:
Options > Text Editor > C# > Formatting > General > Automatically format completed block on }
But you'd probably want to disable Automatically format on paste too.
If you're using ReSharper, Code Editing -> C# -> Formatting Style -> Line Breaks and Wrapping has an option "Place abstract/auto property/indexer/event on single line" that controls the behavior you describe.
Tools -> Options -> Text editor has a lot of options for various languages as to how Visual Studio should (or should not) auto-format your code.
Look under Tools -> Options -> Text Editor -> C# -> Formatting.
You might find a setting there that will format it how you want.
EDIT
This is a workaround, but it would do the trick.
Make a code snippet for automagic properties. Here is link with more info on how to do that. It will need a little modification, but you can handle it. ;)
http://msmvps.com/blogs/kevinmcneish/archive/2007/04/30/property-code-snippet-for-visual-studio-2005.aspx
Do you have coderush or any other code generating addons installed?
Note - I believe this was https://github.com/dotnet/roslyn/issues/2837 and fixed in VS2015 Update 1.

Categories