C# Switch 语句带/不带花括号...有什么区别?
C# 是否始终允许您在 case:
语句之间的 switch()
语句内省略大括号?
像 JavaScript 程序员经常做的那样,省略它们会产生什么影响?
例子:
switch(x)
{
case OneWay:
{ // <---- Omit this entire line
int y = 123;
FindYou(ref y);
break;
} // <---- Omit this entire line
case TheOther:
{ // <---- Omit this entire line
double y = 456.7; // legal!
GetchaGetcha(ref y);
break;
} // <---- Omit this entire line
}
Has C# always permitted you to omit curly brackets inside a switch()
statement between the case:
statements?
What is the effect of omitting them, as javascript programmers often do?
Example:
switch(x)
{
case OneWay:
{ // <---- Omit this entire line
int y = 123;
FindYou(ref y);
break;
} // <---- Omit this entire line
case TheOther:
{ // <---- Omit this entire line
double y = 456.7; // legal!
GetchaGetcha(ref y);
break;
} // <---- Omit this entire line
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
大括号不是必需的,但它们可能会在引入新的声明空间时派上用场。据我所知,自 C# 1.0 以来,这种行为没有改变。
省略它们的效果是,在 switch 语句中某处声明的所有变量从其声明点在所有 case 分支中都是可见的。
另请参阅 Eric Lippert 的示例(帖子中的案例 3):
Eric的例子:
这个无法编译,因为
int y
和double y
位于switch
语句引入的同一声明空间中。您可以通过使用大括号分隔声明空格来修复错误:Curly braces are not required, but they might come in handy to introduce a new declaration space. This behavior hasn't changed since C# 1.0 as far as I know.
The effect of omitting them is that all variables declared somewhere inside the
switch
statement are visible from their point of declaration throughout all case branches.See also Eric Lippert's example (case 3 in the post):
Eric's example:
This does not compile because
int y
anddouble y
are in the same declaration space introduced by theswitch
statement. You can fix the error by separating the declaration spaces using braces:大括号是开关块,它们不是开关部分的一部分。大括号可以插入到 switch 部分中,或者同等地插入到任何地方以控制代码中的范围。
它们对于限制开关块内的范围很有用。例如:
vs...
注意:在使用之前,C# 将要求您在第一个示例中的 case 5 块中为 y 设置一个值。这是防止意外变量跟随的保护。
The curly braces are an optional part of the switch block, they are not part of the switch sections. Braces can be inserted within switch sections or equally inserted anywhere to control scope in your code.
They can be useful to limit scope within the switch block. For example:
vs...
Note: C# will require you to set a value to y within the case 5 block in the first example before using it. This is protection against accidental variable follow through.
开关内部的大括号实际上根本不是开关结构的一部分。它们只是作用域块,您可以将其应用到代码中任何您喜欢的位置。
有它们和没有它们之间的区别在于每个块都有它自己的范围。您可以在作用域内声明局部变量,这不会与另一个作用域中的其他变量冲突。
例子:
The braces inside the switch is actually not part of the switch structure at all. They are just scope blocks, which you can apply in code anywhere you like.
The difference between having them and not having them is that each block has it's own scope. You can declare local variables inside the scope, that doesn't conflict with other variables in another scope.
Example:
它们并不是绝对必要的,但是一旦您开始声明局部变量(在 switch 分支中),我们就强烈推荐它们。
这行不通:
这可以编译,但很诡异:
所以这是最好的:
只要保持简单和可读即可。您不想要求读者详细了解中间示例中涉及的规则。
They are not strictly necessary but as soon as you start declaring local variables (in the switch branches) they are very much recommended.
This won't work:
This compiles but it's spooky:
So this is best:
Just keep it simple and readable. You don't want to require readers to have a detailed knowledge of the rules involved in the middle example.