c# switch 语句比 vb.net 'case' 更受限制

发布于 2024-11-05 10:23:04 字数 661 浏览 2 评论 0原文

我正在此处阅读一篇有趣的文章它提出了一个关于 vb.net 中的“case”语句与 C# 中的“switch”语句的有趣观点,我将其粘贴在下面:

以下 Visual Basic Select Case 语句不能用单个 switch 在 C# 中表示语句:

Dim Condition As Integer = 55
Select Case Condition
  Case 1, 3 To 5, 10, 12, 14, Is > 50
    'value 55 executes code here
  Case Else
    'values <1, 2, 6-9, 11, 13, 15-49
End Select

我总是发现 C# 中的 switch 语句,在每种情况下都带有 dropthrough 和随后的中断要求,有点笨拙。他们有什么理由没有增强 switch 命令来允许这些情况吗?到底什么时候 dropthrough 会有用呢?有人知道该结构的任何扩展以提供更大的灵活性吗?

干杯

I was reading an interesting article here and it made an interesting point about the 'case' statement in vb.net vs the 'switch' statement in C#, which I've pasted below:

The following Visual Basic Select Case statement can't be represented in C# with a single switch statement:

Dim Condition As Integer = 55
Select Case Condition
  Case 1, 3 To 5, 10, 12, 14, Is > 50
    'value 55 executes code here
  Case Else
    'values <1, 2, 6-9, 11, 13, 15-49
End Select

I've always found the switch statement in C#, with dropthrough and consequentrequirements for a break in each case, to be a bit unwieldy. Is there any reason they haven't enhanced the switch command to allow these situations? When would dropthrough be useful anyway? Anyone know of any extensions of the construct to allow more flexibility?

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

时光是把杀猪刀 2024-11-12 10:23:04

在 C# 中,只能在情况下使用不同的值。这使得它更加受到限制,但另一方面它使它变得更快,因为它可以使用哈希查找来实现。

C# 中的 switch 语法比 C/C++ 中的限制更严格。您仍然可以做同样的事情,但是不会隐式地进行失败,您必须编写到下一个案例的特定跳转。之所以有这样的限制,是因为错误地摔倒比故意摔倒更常见。

在 C# 中,默认情况下您需要一个 if 语句来处理范围:

int condition = 55;
switch (condition) {
  case 1:
  case 3:
  case 4:
  case 5:
  case 10:
  case 12:
  case 14:
    // values 1, 3-5, 10, 12, 14
    break;
  default:
    if (condition > 50) {
      // value 55 executes code here
    } else {
      // values <1, 2, 6-9, 11, 13, 15-49
    }
    break;
}

In C# you can only use distinct values in cases. This makes it more limited, but on the other hand it makes it faster because it can be implemented using a hash lookup.

The switch syntax has been made more restricted in C# than in C/C++. You can still do the same things, but a fall through is not made implicitly, you have to write a specific jump to the next case. The reason for this restriction is that it's much more common to do fall through by mistake than intentionally.

In C# you would need an if statement in the default case to handle the ranges:

int condition = 55;
switch (condition) {
  case 1:
  case 3:
  case 4:
  case 5:
  case 10:
  case 12:
  case 14:
    // values 1, 3-5, 10, 12, 14
    break;
  default:
    if (condition > 50) {
      // value 55 executes code here
    } else {
      // values <1, 2, 6-9, 11, 13, 15-49
    }
    break;
}
萌逼全场 2024-11-12 10:23:04

我记得一位大学讲师曾经告诉我们,他发现应对失败的唯一有用的事情就是写下圣诞节十二天的歌词。

沿着这些思路

for (int i = 1; i <= 5; i++) {
    Console.WriteLine("On the " + i + " day of christmast my true love gave to me");
    switch (i) {
    case 5:
        Console.WriteLine("5 Gold Rings");
        goto case 4;
    case 4:
        Console.WriteLine("4 Colly Birds");
        goto case 3;
    case 3:
        Console.WriteLine("3 French Hens");
        goto case 2;
    case 2:
        Console.WriteLine("2 Turtle Doves");
        goto case 1;
    case 1:
        Console.WriteLine("And a Partridge in a Pear Tree");
        break;
    }
    Console.WriteLine("-");
}

10年后我开始同意他的观点。当时我们正在做java,但确实失败了,不得不为C#伪造它。

I remember a uni lecturer once telling us the only useful thing he had ever found to do with fall through was write out the lyrics to the twelve days of christmas.

Something along these lines

for (int i = 1; i <= 5; i++) {
    Console.WriteLine("On the " + i + " day of christmast my true love gave to me");
    switch (i) {
    case 5:
        Console.WriteLine("5 Gold Rings");
        goto case 4;
    case 4:
        Console.WriteLine("4 Colly Birds");
        goto case 3;
    case 3:
        Console.WriteLine("3 French Hens");
        goto case 2;
    case 2:
        Console.WriteLine("2 Turtle Doves");
        goto case 1;
    case 1:
        Console.WriteLine("And a Partridge in a Pear Tree");
        break;
    }
    Console.WriteLine("-");
}

10 years later I tend to agree with him. At the time we were doing java which does fall through, had to fake it for C#.

屋檐 2024-11-12 10:23:04

对于匹配多个案例的特殊情况,允许直接删除,但不允许比较和范围案例。所以:

int condition = 55;
switch (condition) {
  case 1: case 3: case 4: case 5: case 10: case 12: case 14:
    // value 55 doesn't execute here anymore

  default:
    //values <1, 2, 6-9, 11, 13, >14
}

Drop through is allowed for the special case of matching multiple cases, but the comparative and range cases aren't allowed. So:

int condition = 55;
switch (condition) {
  case 1: case 3: case 4: case 5: case 10: case 12: case 14:
    // value 55 doesn't execute here anymore

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