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

*爱你&永不变心* 提交于 2019-12-10 23:35:02

问题


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"



来源:https://stackoverflow.com/questions/43768743/visual-studio-2017-c-sharp-formatting-one-line-if-statements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!