如果没有大括号的代码块,C#是否支持?

Kev*_*oyd 14 c# compilation codeblocks conditional-statements

C#如何编译?

if (info == 8)
    info = 4;
otherStuff();
Run Code Online (Sandbox Code Playgroud)

它会包含代码块中的后续行吗?

if (info == 8)
{
    info = 4;
    otherStuff();
}
Run Code Online (Sandbox Code Playgroud)

或者它只需要下一行?

if (info == 8)
{
    info = 4;
}
otherStuff();
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 43

是的,它支持它 - 但它需要下一个声明,而不是下一.例如:

int a = 0;
int b = 0;
if (someCondition) a = 1; b = 1;
int c = 2;
Run Code Online (Sandbox Code Playgroud)

相当于:

int a = 0;
int b = 0;
if (someCondition)
{
    a = 1;
}
b = 1;
int c = 2;
Run Code Online (Sandbox Code Playgroud)

就个人而言,我总是在if声明的主体周围加上括号,而且我遇到的大多数编码约定采用相同的方法.

  • 最佳答案:包括解释,代码片段和陷阱! (6认同)

小智 7

if (info == 8)
{
    info = 4;
}
otherStuff();
Run Code Online (Sandbox Code Playgroud)