Did C# formatting change in Visual Studio 2015? And how can I change it back?
Asked Answered
G

2

53

In past versions of Visual Studio, I could create a single-line autoproperty in C# like this:

public int Whatever { get; set; }

If I hit Control-K, Control-D to format, the property would stay that way.

But in Visual Studio 2015 RC, when I type the property, it wraps, and even if I unwrap it, formatting wraps it again:

public int Whatever
{ get; set; }

I've noticed it with constructors as well. In the past, an empty constructor (e.g. that just called a base class constructor) could look like this:

public Whatever(int stuff)
    : base(stuff) { }

Now Visual Studio 2015 insists on doing this:

public Whatever(int stuff)
    : base(stuff)
{ }

Have others noticed this? Is this a change made in Visual Studio 2015? If so, is there a way I can change it back? I looked through the C# formatting section of Tools > Options, but couldn't find any new setting that might affect this.

(It's not impossible that one of my add-ins is causing it, but I didn't find any obvious culprits.)

(Why even care? Because when I use the Collapse to Definitions outlining command, single-line properties and constructors stay as they are, whereas wrapped ones collapse. If they're collapsed, I can't tell at a glance that they're empty; I have to toggle them to uncollapsed just to see that nothing's there.)

Gilmore answered 29/5, 2015 at 3:45 Comment(7)
to be honest: I cannot remember if VS does this but there is an option in ReSharper for this so if you are using it (and maybe you do not yet for VS2015) then this could be the culprit - cannot say right now because this time I did not play with the new version yet - maybe when it hit RTMHollandia
sir,which edition of vs15?Gaskins
@utility does this matter for the new ones? Formatting used to be the same for all AFAIKHollandia
@Carsten, I'm not using Resharper. I do use CodeRush, but I've used it for ages and it doesn't appear to be causing this.Gilmore
I've posted a UserVoice suggestion about this problem: Empty braces are no longer formatted correctly with the FormatDocument command. If you found your way here because this problem bothers you as well, please consider voting for it.Gilmore
This is still broken in VS2015 RTM. I've posted another user voice bug with a more obvious repro here visualstudio.uservoice.com/forums/121579-visual-studio/…Laurynlausanne
"Open brace after else" is broken on VS2015 update 1. :( :(Bivens
D
40

Go to Tools > Options > Text editor > C# > Formatting > Wrapping

Check "Leave block on single line" and "Leave statements and member declarations on the same line"

enter image description here

Dowager answered 29/5, 2015 at 6:15 Comment(6)
Note that if you don't have the second box checked, this won't help at all. With just the first box checked, VS2015 will put a new line in the middle of int Foo { get; set; } directly after 'Foo'Smalto
Unfortunately this does not help when typing out a property. Once the property is on a single line it will stay on a single line. What you can do is after you type "get;" hit ctrl+z to undo the formatting and continue on like normal.Numerology
@AlexJorgenson Good call on the Ctrl+Z, but man does that suck!Collectanea
I have changed the explanation to reflect VS2015 behavior.Dowager
Instead of using Ctrl+Z if you create opening and closing braces before you add the get; set; it won't cause that trouble and you can avoid the Ctrl+Z which is more painful than backing up a space IMHO, plus Ctrl+Z can have unintended side effects if your focus gets stolen.Phratry
It seems if the "automatic brace completion" (under the 'All languages' option) is turned fully on then the property stays on the same line as you type it, because the closing brace is added automatically before typing the 'get;' but this has it's own disadvantages in other places.Ancillary
G
4

This behavior does appear to have changed. Go to the Tools > Options menu, and then navigate to Text Editor > C# > Formatting > Wrapping.

In previous versions of Visual Studio, if you had "Leave block on single line" checked and "Leave statements and member declarations on the same line" unchecked, empty braces would stay on the same line if you put them there.

But in Visual Studio 2015 RC, if you have "Leave block on single line" checked and "Leave statements and member declarations on the same line" unchecked, the empty braces are wrapped.

You have to have both items checked to prevent the braces from wrapping. But this also has other consequences, such as leaving multiple statements on the same line...

int x = 5; int y = 3;

...which is why I never had it checked before.

Gilmore answered 29/5, 2015 at 14:25 Comment(3)
Alas, this prevents the property wrapping, but it doesn't prevent the empty constructor/method wrapping.Gilmore
@Kralessa If you go into Tools > Options > Text editor > C# > Formatting -> General and uncheck Automatically format satment on ; that will let you type it all out on a single line without auto wrapping. This is the only way, I have found, to stop that.Sfax
@Matthew, yes, but then if you later format the whole file is goes to the bad place...Teddy

© 2022 - 2024 — McMap. All rights reserved.