简体   繁体   English

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

[英]In VB.NET why should I use Select, instead of If?

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.在我们的培训中,他们向我们展示了 VB.NET 以及他们在这里使用的许多功能。 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).在一些示例中,他们使用Select语句(在一些地方,它们被用于真正应该使用If/Else的地方)。

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.我唯一一次在其他语言中使用 switch/select 语句(除了需要它的分配)是当我想要下一个语句时。

Given than VB.NET has no fall through, what (if any) cases are there to use the Select statement?鉴于 VB.NET 没有失败,有哪些(如果有)案例可以使用Select语句? Are there any cases when it provides advantages over and If/ElseIf statement?是否有任何情况下它提供优于If/ElseIf语句的优势?

Select Case , not just Select . Select Case ,不仅仅是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.它优于 C/C++ switch ,因为它允许表达式作为分支点,而不仅仅是常量。

  5. When using constants as branching points (example 1), compiler is able to generate a more optimised code with direct jumps.当使用常量作为分支点(示例 1)时,编译器能够通过直接跳转生成更优化的代码。

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. Select告诉编译器,类似的 If/Else 块集中的每个比较 (If) 都在相同的值上,这允许它进行某些更难确定的优化。 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).例如,它可能更渴望生成将该值保存在 cpu 寄存器中的机器代码(这只是假设......不同的编译器可以做他们想做的事)。

Also, some of us find Select a lot more readable.此外,我们中的一些人发现Select更具可读性。 It is important to follow the coding standards of whatever team or unit you find yourself.遵循您发现自己的任何团队或单位的编码标准非常重要。

First off, VB does have fall through, it's just not as obvious.首先,VB 确实失败了,只是不那么明显。 The "fallthrough" in VB is just setting one case to have multiple values: VB 中的“失败”只是将一种情况设置为具有多个值:

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.至于它的优点,编写一个Select语句比说三个以上的If/ElseIf语句都针对相同的值进行测试要容易得多。

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.如果您要根据输入比较/范围比较做几件不同的事情,如果它是 if-elseif+,那么使用Select而不是疯狂的 if-elseif 块。

VB.NET's Select statement has some cool features as well, so ensure that you know all the features. VB.NET 的 Select 语句也有一些很酷的特性,因此请确保您了解所有特性。

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.在某些情况下,Select Case 可能比 If 更有效:当您在 If 条件中有一个“或”子句列表并且您不需要全部评估它们来确定条件的真实性时。 Suppose you had an if statement such as this:假设你有一个这样的 if 语句:

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() 和 B() 都被执行,并且当 A() 和 B() 都为假时,对于第二个 if 语句也是如此。 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.如果 A() 返回 true,则 B() 的值无关紧要,除非它以您真正想要的方式更改程序 state(通常不是好的做法),否则 B() 的执行是多余的。 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.您可以将条件分成多个 IfElse 语句以自行优化,但这会降低代码的可读性,并增加稍后进行更改时出错的危险。 I find that using Select Case is better in this situation:我发现在这种情况下使用 Select Case 更好:

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.现在,如果 A() 返回 True 那么 B() 根本不会被评估。 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).条件的评估按列出的顺序进行,因此您可以通过将测试按最有可能返回 True 或执行成本最低的顺序排列(取决于应用程序)来帮助优化代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM