它应该出现在之后。
例如:
switch(value)
{
case 0:
{
// this ...
// that ...
// and the other ...
}
break;
}
编辑下方的文字
这主要是为了提高可读性和可维护性,这是一个示例。
switch (value)
{
case 0:
// Do this...
// Do that...
break;
case 1:
//and the other...
break;
}
和
switch (value)
{
case 0:
// Do this...
// Do that...
if (ObjectWithinScope.Type == Fault)
break;
case 1:
//and the other...
break;
}
现在比较
switch (value)
{
case 0:
{
// Do this...
// Do that...
}
break;
case 1:
//and the other...
break;
}
和
switch (value)
{
case 0:
{
// Do this...
// Do that...
if (ObjectWithinScope.Type == Fault)
break;
}
case 1:
{
//and the other...
}
break;
}
当您开始遇到嵌套 switch 语句的情况时,确实会变得非常混乱。
只是一个指针。
现在你们中的一些人仍然想知道我在说什么。这里是。一段遗留代码停止工作,没有人能弄清楚原因。这一切都归结为一段结构如下的代码:
switch (value)
{
case 0:
{
// Do this...
// Do that...
if (ObjectWithinScope.Type == Fault)
break;
}
case 1:
{
//and the other...
}
break;
}
这段代码花了很长时间才确定下来,但是在检查更改日志时,原来是这样的:
switch (value)
{
case 0:
{
// Do this...
// Do that...
if (ObjectWithinScope.Type == Fault)
// *** A line of code was here ***
break;
}
case 1:
{
//and the other...
}
break;
}
诚然,原始代码与其自身不一致,但是通过在大括号内添加中断,代码在一行代码被意外删除时编译。如果中断在括号之外,则不会。