C# Switch 语句带/不带花括号...有什么区别?

发布于 2024-09-17 21:01:35 字数 595 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦幻之岛 2024-09-24 21:01:35

大括号不是必需的,但它们可能会在引入新的声明空间时派上用场。据我所知,自 C# 1.0 以来,这种行为没有改变。

省略它们的效果是,在 switch 语句中某处声明的所有变量从其声明点在所有 case 分支中都是可见的。

另请参阅 Eric Lippert 的示例(帖子中的案例 3):

四个开关怪异< /p>

Eric的例子:

switch(x)
{
  case OneWay:
    int y = 123;
    FindYou(ref y);
    break;
  case TheOther:
    double y = 456.7; // illegal!
    GetchaGetcha(ref y);
    break;
}

这个无法编译,因为 int ydouble y 位于 switch 语句引入的同一声明空间中。您可以通过使用大括号分隔声明空格来修复错误:

switch(x)
{
  case OneWay:
  {
    int y = 123;
    FindYou(ref y);
    break;
  }
  case TheOther:
  {
    double y = 456.7; // legal!
    GetchaGetcha(ref y);
    break;
  }
}

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):

Four switch oddities

Eric's example:

switch(x)
{
  case OneWay:
    int y = 123;
    FindYou(ref y);
    break;
  case TheOther:
    double y = 456.7; // illegal!
    GetchaGetcha(ref y);
    break;
}

This does not compile because int y and double y are in the same declaration space introduced by the switch statement. You can fix the error by separating the declaration spaces using braces:

switch(x)
{
  case OneWay:
  {
    int y = 123;
    FindYou(ref y);
    break;
  }
  case TheOther:
  {
    double y = 456.7; // legal!
    GetchaGetcha(ref y);
    break;
  }
}
2024-09-24 21:01:35

大括号是开关块,它们不是开关部分的一部分。大括号可以插入到 switch 部分中,或者同等地插入到任何地方以控制代码中的范围。

它们对于限制开关块内的范围很有用。例如:

int x = 5;

switch(x)
{
case 4:
    int y = 3;
    break;
case 5:
    y = 4;
    //...                      
    break;
}

vs...

int x = 5;

switch(x)
{
  case 4:
  {
    int y = 3;
    break;
  }
  case 5:
  {
    y = 4;//compiling error
    //...                      
    break;
  }
}

注意:在使用之前,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:

int x = 5;

switch(x)
{
case 4:
    int y = 3;
    break;
case 5:
    y = 4;
    //...                      
    break;
}

vs...

int x = 5;

switch(x)
{
  case 4:
  {
    int y = 3;
    break;
  }
  case 5:
  {
    y = 4;//compiling error
    //...                      
    break;
  }
}

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.

兮子 2024-09-24 21:01:35

开关内部的大括号实际上根本不是开关结构的一部分。它们只是作用域块,您可以将其应用到代码中任何您喜欢的位置。

有它们和没有它们之间的区别在于每个块都有它自己的范围。您可以在作用域内声明局部变量,这不会与另一个作​​用域中的其他变量冲突。

例子:

int x = 42;
{
  int y = x;
}
{
  int y = x + 1; // legal, as it's in a separate scope
}

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:

int x = 42;
{
  int y = x;
}
{
  int y = x + 1; // legal, as it's in a separate scope
}
烟─花易冷 2024-09-24 21:01:35

它们并不是绝对必要的,但是一旦您开始声明局部变量(在 switch 分支中),我们就强烈推荐它们。

这行不通:

    // does not compile
    switch (x)
    {
        case 1 :               
            int j = 1;
            ...
            break;

        case 3:
            int j = 3;   // error
            ...
            break;
    }

这可以编译,但很诡异:

    switch (x)
    {
        case 1 :               
            int j = 1;
            ...
            break;

        case 3:
            j = 3;
            ...
            break;
    }

所以这是最好的:

  switch (x)
    {
        case 1 : 
         {             
            int j = 1;
            ...
         }
         break;  // I prefer the break outside of the  { }

        case 3: 
         {
            int j = 3;
            ...
         }
         break;
    }

只要保持简单和可读即可。您不想要求读者详细了解中间示例中涉及的规则。

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:

    // does not compile
    switch (x)
    {
        case 1 :               
            int j = 1;
            ...
            break;

        case 3:
            int j = 3;   // error
            ...
            break;
    }

This compiles but it's spooky:

    switch (x)
    {
        case 1 :               
            int j = 1;
            ...
            break;

        case 3:
            j = 3;
            ...
            break;
    }

So this is best:

  switch (x)
    {
        case 1 : 
         {             
            int j = 1;
            ...
         }
         break;  // I prefer the break outside of the  { }

        case 3: 
         {
            int j = 3;
            ...
         }
         break;
    }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文