locked
Difference between And and AndAlso???

    Question

  • Whats the performance difference or any difference when building if statements using AndAlso, OrElse instead of Or and And?????
    Friday, February 02, 2007 3:20 AM

Answers

  • The AndAlso/OrElse operators also provide "correctness" benefits:

    Dim c As String = Nothing

    If c is not nothing AndAlso c.SubString(...) then

    In this case, if c is nothing, the second clause does not execute. If you had done:

    If c is not nothing And c.SubString(...) then

    In this case, even if c is nothing, the second clause executes, and then you will have a NullReferenceException because you can't call SubString() on c which his Nothing.
    Friday, February 02, 2007 7:45 PM
  • Hi

    AndAlso, OrElse are probably the preferred method in 99.9% of cases ...

    Consider the following code

    If x > y and me.doalotofwork(x) then .....

    In this case the method, doalotofwork will be called even if x <= y.  Now it might be that this is the behaviour you want because your code depends upon the side effects that this method call generates, but most of the time the call would be unnecessary.

    If you used AndAlso, the method would not be called if the first expression returned false.

    In terms of performance, the benefits are clear ... if you dont need to resolve an expression, it's better not to try to do so.

    OrElse is the opposite in that the expression attached to the right hand side of this operator will only be resolved if the left hand side returned false.

    Give me a shout if any of that is not clear.

    Richard

    Friday, February 02, 2007 9:41 AM

All replies

  • Hi

    AndAlso, OrElse are probably the preferred method in 99.9% of cases ...

    Consider the following code

    If x > y and me.doalotofwork(x) then .....

    In this case the method, doalotofwork will be called even if x <= y.  Now it might be that this is the behaviour you want because your code depends upon the side effects that this method call generates, but most of the time the call would be unnecessary.

    If you used AndAlso, the method would not be called if the first expression returned false.

    In terms of performance, the benefits are clear ... if you dont need to resolve an expression, it's better not to try to do so.

    OrElse is the opposite in that the expression attached to the right hand side of this operator will only be resolved if the left hand side returned false.

    Give me a shout if any of that is not clear.

    Richard

    Friday, February 02, 2007 9:41 AM
  • There are differences between the Andalso and OrElse operators both in terms of potential performance and functionality.

    The following article explains a little about the new operators which were only added in VB.NET 2005 and was written by one of the architects of the VB Language.   This should explain a little bit of the differences and why these new operators were added.

    http://www.panopticoncentral.net/archive/2003/08/18/179.aspx

    The new operators provide something called short circuiting which is explained a little in the article.   This has the ability to provide a performance benefit.

    Both sets of operators have there place - but you need to determine which is applicable to your functionality you are trying to implement.   Its difficult to make a blanket statement which says yes this is better as it will almost certainly never apply to all scenarios.

    For performance - is it going to make a huge performance difference (probably not) unless you have something that is executing a huge number of times, when you may notice some difference but that said a badly coded solution using a function which is potentially quicker may still perform worse than a well coded solution using a slower function.   

     

     

     

    Friday, February 02, 2007 6:16 PM
  • The AndAlso/OrElse operators also provide "correctness" benefits:

    Dim c As String = Nothing

    If c is not nothing AndAlso c.SubString(...) then

    In this case, if c is nothing, the second clause does not execute. If you had done:

    If c is not nothing And c.SubString(...) then

    In this case, even if c is nothing, the second clause executes, and then you will have a NullReferenceException because you can't call SubString() on c which his Nothing.
    Friday, February 02, 2007 7:45 PM
  • Spotty,

     

    I'm a new guy here...new to Visual Basic.  Am I to understand that the use of AndAlso and OrElse primarily provide performance benefits in that unnecessary code is not evaluated?  It seems like this is generally something that goes on well beyond the view of the user and that, in most cases, a user would never know (or care) that an AndAlso statement was used as opposed to an And statement.  Would it be a good practice to exclusively use AndAlso and OrElse, unless a specific case for using And or Or came up?  This question seems to have been answered a number of different ways, but I'm trying to get my head around the theory behind the use of these conditional statements.  Sorry if this sounds redundant.  But, your insight and opinion is appreciated and valued.

    Thursday, October 25, 2007 7:38 PM
  • For most purposes, you'll want to use AndAlso and OrElse.

    'And' and 'Or' still have an important purpose though - for bitwise operations (these are '&' and '|' in C#).

    Friday, October 26, 2007 12:04 AM