C# 会如何编译这段代码?
if (info == 8)
info = 4;
otherStuff();
它会包括代码块中的后续行吗?
if (info == 8)
{
info = 4;
otherStuff();
}
还是只需要下一行?
if (info == 8)
{
info = 4;
}
otherStuff();
C# 会如何编译这段代码?
if (info == 8)
info = 4;
otherStuff();
它会包括代码块中的后续行吗?
if (info == 8)
{
info = 4;
otherStuff();
}
还是只需要下一行?
if (info == 8)
{
info = 4;
}
otherStuff();
是的,它支持 - 但它接受下一个语句,而不是下一行。例如:
int a = 0;
int b = 0;
if (someCondition) a = 1; b = 1;
int c = 2;
等价于:
int a = 0;
int b = 0;
if (someCondition)
{
a = 1;
}
b = 1;
int c = 2;
个人而言,我总是在 if
语句的主体周围加上大括号,而且我遇到的大多数编码约定都采用了同样的做法。
if (info == 8)
{
info = 4;
}
otherStuff();
if( true )
method1();
method2();
将会和原来一样
if( true )
{
method1();
}
method2();
是的,它支持没有大括号的if代码块,只有在if后的第一条语句将被包含在if块中,就像您的第二个示例中一样