在 VB.NET 中为什么应该使用 Select 而不是 If?
我最近毕业并开始了一份真正的工作。在我们的培训中,他们向我们展示了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
选择案例
,而不仅仅是选择
。对我来说,这是该语言最好的功能之一。
当您有多个可能的值可供测试时,它会更加直观。
在测试范围方面它更具可读性:
当您有一堆更复杂的条件要测试时,它会更具可读性,并确保只选择一个条件:
它优于 C/C++
switch
它允许表达式作为分支点,而不仅仅是常量。当使用常量作为分支点时(示例1),编译器能够通过直接跳转生成更优化的代码。
Select Case
, not justSelect
.To me, it's one of the best features of the language.
It's much more visual when you have several possible values to test against.
It's much more readable when it comes to testing ranges:
It's much more readable when you have a bunch of more complex conditions to test, making sure only one is selected:
It's superior to C/C++
switch
in the sense that it allows expressions as branching points, not just constants.When using constants as branching points (example 1), compiler is able to generate a more optimised code with direct jumps.
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.首先,VB 确实失败了,只是不那么明显。 VB 中的“失败”只是将一种情况设置为具有多个值:
至于它的优点,编写一个
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:
As for its advantages, it's way easier to write one
Select
statement than say, more than threeIf/ElseIf
statements that all test against the same value.如果您要根据输入比较/范围比较(如果是 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.
在某些情况下,Select Case 比 If 更高效:当 If 条件中有一系列“Or”子句时,您不需要对它们全部进行评估来确定条件的真实性。假设您有这样的 if 语句:
在本例中,为了计算第一个 if 语句,函数 A() 和 B() 都被执行,对于第二个 if 语句也类似,当 A() 和 B() 都为错误的。如果 A() 返回 true,则 B() 的值无关紧要,并且除非它以您实际想要的方式更改程序状态(通常不是好的做法),否则 B() 的执行是多余的。编译器受到以下要求的限制:在得出值结论之前必须执行测试的所有部分(根据语言规范不允许优化测试)。
您可以将条件分成多个 IfElse 语句来自行优化,但这会降低代码的可读性,并增加稍后进行更改时出错的危险。我发现在这种情况下使用 Select Case 更好:
现在,如果 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:
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:
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).