Visual Studio 2017 C#格式:单行If语句 [英] Visual Studio 2017 C# Formatting: One-Line If Statements
问题描述
在Visual Studio 2017 for C#中,是否有一种方法可以在一行上保留if语句,而不影响其他控制块的格式? 文本编辑器> C#>代码样式>格式>换行"下的在控制行的新行上放置大括号"规则将允许if语句保留在一行上,但也会强制所有其他控制块具有大括号在同一行上.
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; }
与其自动格式化为:
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)
{
....
}
推荐答案
是的,如elgonzo的评论所建议的那样,行为是通过转到工具"来控制的.菜单,然后选择选项->文字编辑器-> C#->代码样式->格式化->包装纸
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
在同一行上检查"Leave语句和成员声明"
Check "Leave statement and member declarations on the same line"
这篇关于Visual Studio 2017 C#格式:单行If语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!