Matt Kemmer
Matt Kemmer

Reputation: 111

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

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)
{
    ....
}

Upvotes: 11

Views: 5339

Answers (1)

Patrick Montelo
Patrick Montelo

Reputation: 2421

Yes, as suggested by elgonzo's comment that behavior is controlled by going to the "Tools" menu, then Options -> Text Editor -> C# -> Code Style -> Formatting -> Wrapping

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

Upvotes: 14

Related Questions