Visual Studio 2017 C# Formatting: One-Line If Stat

2019-07-13 20:23发布

问题:

In Visual Studio 2017 for C#, is there a way to leave one-line if statements on one line without affecting the formatting of other control blocks? The "Place open brace on new line for control blocks" rule under "Text Editor > C# > Code Style > Formatting > New Lines" will allow if statements to remain on one line, but it will also force all other control blocks to have the curly bracket on the same line.

For example, I'd like the following statement's formatting to be left alone:

if(x == null) { return; }

Instead of being auto-formatted to this:

if(x == null)
{ return; }

While also allowing other control statements to keep their curly bracket on the next line like this:

foreach(string s in strings)
{
    ....
}

回答1:

Yes, as suggested by elgonzo's comment that's controlled by Options -> Text Editor -> C# -> Code Style -> Formatting -> Wrapping

Check "Leave statement and member declarations on the same line"