且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-12-27 08:32:16

是的,它支持它 - 但它需要下一个 statement,而不是下一个 line.比如:

Yes, it supports it - but it takes the next statement, not the next line. So for example:

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 语句的主体周围加上大括号,而且我遇到的大多数编码约定都采用相同的方法.

Personally I always include braces around the bodies of if statements, and most coding conventions I've come across take the same approach.