为什么AndAlso不是更换,以和 [英] why AndAlso is not a replacement to And

查看:141
本文介绍了为什么AndAlso不是更换,以和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/55013/should-i-always-use-the-andalso-and-orelse-operators">Should我总是使用AndAlso和OrElse运算经营者?

在实际执行中的各种情况,逻辑EX pression将返回这两个相同的布尔值 AndAlso

In a practical implementation for all scenarios, the logical expression will return the same boolean value for both And and AndAlso,

为什么不使用我们始终 AndAlso 并保存处理时间为下一个条件时,previous是假的?换句话说,为什么仍在使用?

why not we use always AndAlso and save the processing time for the next condition when the previous is false? In other words, Why And is still in use?

推荐答案

在VB.NET中与运营商进行的两个的工作,它既是一个逻辑和数学运算符。早期版本的Visual Basic,两者之间并没有区别。按照设计,该语言被设计成易于使用和强制Visual Basic程序员学习差异的东西,语言设计者想避免。

The And operator in VB.NET performs two jobs, it is both a logical and a mathematical operator. Early versions of Visual Basic did not distinguish between the two. By design, the language was designed to be easy-to-use and forcing Visual Basic programmers to learn the difference was something the language designers wanted to avoid.

这工作pretty的不错,虽然它给了语言中的一些怪癖。真例如值不是1,像它在许多语言中,它是-1。从CINT(真)获得的价值。这允许忽略两种用途的操作者之间的差,但它仍然运行良好时如果()语句使用而当左侧为整数,右侧是布尔例如

That worked pretty well, although it gave the language a few quirks. The value of True for example is not 1, like it is in many languages, it is -1. The value you get from CInt(True). Which allows ignoring the difference between the two uses of the operator, it still works well when an If() statement uses And when the left side is an Integer and the right side is a Boolean for example.

但是,有一个pretty的具体问题与与运营商扮演两个角色。一个常见的​​用法是写如下语句:

But there's a pretty specific problem with the And operator playing both roles. A common usage is to write a statement like this:

If ix < array.Length And array(ix) <> 42 Then
   '' do something
End If

这是一个声明,这将使你的code坠毁与IndexOutOfRangeException。你什么的意味着的是如果该指数超出范围,则不用检查数组元素。这就是所谓的短路计算。但是,这并不是什么而操作者,它计算左边和右边前pressions。像的数学的版本,而且运营商必须要做的。

That's a statement that will make your code crash with an IndexOutOfRangeException. What you meant is "if the index is out of bounds then don't bother checking the array element". That's called "short-circuit evaluation". But that's not what the And operator does, it evaluates both the left and the right expressions. Like a mathematical version of the And operator must do.

短路评价是重要的,已经存在很长时间。而普遍采用的花括号语言,开始与C.它终于得到了采纳VB.NET以及,你写这样避免了异常:

Short-circuit evaluation is important and has been around a long time. And universally adopted in the curly-brace languages, starting with C. And it finally got adopted in VB.NET as well, you write it like this to avoid the exception:

If ix < array.Length AndAlso array(ix) <> 42 Then
   '' do something
End If

因此​​,要回答你的问题:是的,总是使用AndAlso时,你的意思是使用的逻辑的版本,而运营商。 AndAlso的的逻辑版本。

So to answer your question: Yes, always use AndAlso when you meant to use the logical version of the And operator. AndAlso is the logical version.

这篇关于为什么AndAlso不是更换,以和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆