在 VB.NET 中为什么应该使用 Select 而不是 If?

发布于 2024-11-18 11:15:25 字数 307 浏览 3 评论 0原文

我最近毕业并开始了一份真正的工作。在我们的培训中,他们向我们展示了 VB.NET 以及他们在这里使用的许多功能。在一些示例中,他们使用了 Select 语句(并且在一些确实应该使用 If/Else 的地方使用了它们)。

我唯一一次在其他语言中使用 switch/select 语句(除了需要它的作业)是当我想要跳到下一个语句时。

既然VB.NET没有失败,那么什么情况(如果有的话)可以使用Select语句?在某些情况下,它比 If/ElseIf 语句更有优势吗?

I've recently graduated and started a real job. In our training they've been exposing us to VB.NET and a lot of the features they use here. In some of the examples, they've used Select statements (and in a few places they were used where an If/Else really should have been used).

The only time that I've used a switch/select statement in other languages (other than assignments that required it) has been when I wanted the fall through to the next statement.

Given than VB.NET has no fall through, what (if any) cases are there to use the Select statement? Are there any cases when it provides advantages over and If/ElseIf statement?

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

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

发布评论

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

评论(5

挽容 2024-11-25 11:15:25

选择案例,而不仅仅是选择
对我来说,这是该语言最好的功能之一。

  1. 当您有多个可能的值可供测试时,它会更加直观。

    选择案例 some_var
    案例1
      某物()
    案例2
      别的东西()
    案例3
      ETC()
    结束选择
    
  2. 在测试范围方面它更具可读性:

    选择案例 some_var
    情况1至10
      某物()
    案例20至30
      别的东西()
    情况是> 100
      ETC()
    结束选择
    
  3. 当您有一堆更复杂的条件要测试时,它会更具可读性,并确保只选择一个条件:

    选择 case true
    case string.isnullorempty(a_string)
      某物()
    情况 a_string.length < 5
      别的东西()
    情况 a_string = b_string
      ETC()
    结束选择
    
  4. 它优于 C/C++ switch它允许表达式作为分支点,而不仅仅是常量。

  5. 当使用常量作为分支点时(示例1),编译器能够通过直接跳转生成更优化的代码。

Select Case, not just Select.
To me, it's one of the best features of the language.

  1. It's much more visual when you have several possible values to test against.

    select case some_var
    case 1
      something()
    case 2
      something_else()
    case 3
      etc()
    end select
    
  2. It's much more readable when it comes to testing ranges:

    select case some_var
    case 1 to 10
      something()
    case 20 to 30
      something_else()
    case is > 100
      etc()
    end select
    
  3. It's much more readable when you have a bunch of more complex conditions to test, making sure only one is selected:

    select case true
    case string.isnullorempty(a_string)
      something()
    case a_string.length < 5
      something_else()
    case a_string = b_string
      etc()
    end select
    
  4. It's superior to C/C++ switch in the sense that it allows expressions as branching points, not just constants.

  5. When using constants as branching points (example 1), compiler is able to generate a more optimised code with direct jumps.

铃予 2024-11-25 11:15:25

Select 告诉编译器,类似的 If/Else 块集中的每个比较 (If) 都具有相同的值,这使得编译器能够进行某些难以确定的优化。例如,它可能更渴望生成在 cpu 寄存器中保存该值的机器代码(这只是假设......不同的编译器可以做他们想做的事情)。

此外,我们中的一些人发现 Select 更具可读性。遵循您所在的任何团队或单位的编码标准非常重要。

Select tells the compiler that every compare (If) in the analogous set of If/Else blocks is on the same value, and this allows it to make certain optimizations that are harder to be sure of otherwise. For example, it might be more eager to generate machine code that holds that value in a cpu register (that's just hypothetical... different compilers can do what they want).

Also, some of us find Select a lot more readable. It is important to follow the coding standards of whatever team or unit you find yourself.

素罗衫 2024-11-25 11:15:25

首先,VB 确实失败了,只是不那么明显。 VB 中的“失败”只是将一种情况设置为具有多个值:

Dim number As Integer = 8
Select Case number
    Case 6,7,8
        ' do stuff
    Case Else
        ' do default stuff
End Select

至于它的优点,编写一个 Select 语句比编写三个 If/ElseIf 语句要容易得多code> 语句均针对相同值进行测试。

First off, VB does have fall through, it's just not as obvious. The "fallthrough" in VB is just setting one case to have multiple values:

Dim number As Integer = 8
Select Case number
    Case 6,7,8
        ' do stuff
    Case Else
        ' do default stuff
End Select

As for its advantages, it's way easier to write one Select statement than say, more than three If/ElseIf statements that all test against the same value.

酒与心事 2024-11-25 11:15:25

如果您要根据输入比较/范围比较(如果是 if-elseif+)做几件不同的事情,那么请使用 Select 而不是疯狂的 if-elseif 块。

VB.NET 的 Select 语句也有一些很酷的功能,因此请确保您了解所有功能。

If you are going to do several different things based on the input comparison/range comparison if it's if-elseif+ then use Select instead of crazy if-elseif blocks.

VB.NET's Select statement has some cool features as well, so ensure that you know all the features.

半窗疏影 2024-11-25 11:15:25

在某些情况下,Select Case 比 If 更高效:当 If 条件中有一系列“Or”子句时,您不需要对它们全部进行评估来确定条件的真实性。假设您有这样的 if 语句:

If A() Or B() Then
    DoSomething()
ElseIF C() or D() Then
    DoSomethingElse()
Else
    DoTheDefault()
EndIF

在本例中,为了计算第一个 if 语句,函数 A() 和 B() 都被执行,对于第二个 if 语句也类似,当 A() 和 B() 都为错误的。如果 A() 返回 true,则 B() 的值无关紧要,并且除非它以您实际想要的方式更改程序状态(通常不是好的做法),否则 B() 的执行是多余的。编译器受到以下要求的限制:在得出值结论之前必须执行测试的所有部分(根据语言规范不允许优化测试)。

您可以将条件分成多个 IfElse 语句来自行优化,但这会降低代码的可读性,并增加稍后进行更改时出错的危险。我发现在这种情况下使用 Select Case 更好:

Select Case True
    Case A(), B()
        DoSomething()
    Case C(), D()
        DoSomethingElse()
    Case Else
        DoTheDefault()
End Select

现在,如果 A() 返回 True,则根本不评估 B()。条件的评估按照列出的顺序进行,因此您可以通过按照最有可能返回 True 或执行成本最低的顺序放置测试来帮助优化代码(取决于应用程序)。

There is a situation where Select Case can be much more efficient than If: when you have a list of "Or" clauses in the If condition and you do not need to evaluate them all to establish the truth of the condition. Suppose you had an if statement such as this:

If A() Or B() Then
    DoSomething()
ElseIF C() or D() Then
    DoSomethingElse()
Else
    DoTheDefault()
EndIF

In this case, to evaluate the first if statement, both functions A() and B() are executed, and similarly for the second if statement when A() and B() are both false. If A() returns true, then the value of B() is immaterial and, unless it changes the program state in a way that you actually want it to (generally not good practice), the execution of B() is redundant. The compiler is constrained by the requirement that all parts of the test MUST be executed before concluding on a value (optimisation of the test is not allowed according to the language spec).

You could separate the conditions into multiple IfElse statements to optimise it yourself but this makes the code less readable and increases the danger of errors when changes are made later. I find that using Select Case is better in this situation:

Select Case True
    Case A(), B()
        DoSomething()
    Case C(), D()
        DoSomethingElse()
    Case Else
        DoTheDefault()
End Select

Now, if A() returns True then B() is not evaluated at all. The evaluation of the conditions is in the sequence listed, so you can help to optimise your code by putting the tests in the order of most likely to return True or least expensive to execute (depending on the application).

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