I am not sure if an exception will be thrown, but as far as the
documentation states if the earlier condition is not satisfied the
operation will be short-circuited. And the second parameters won't be
checked.
Hence MS best practice guidelines clear ask you to avoid such
situations.

Always if you *want* the second expression to execute, use the '&'. This
will ensure that both the expressions are executed

Eg

void foo(string bar) {
        //no matter what the second condition will be also evaluated!
         if (bar != null & bar.Length > 3) {
             ...
         } else {
             ...
         }
     }


Regards,
Saurabh Nandu
[Microsoft MVP]
www.MasterCSharp.com
Master C#, the easy way...

> -----Original Message-----
> From: Moderated discussion of advanced .NET topics. [mailto:ADVANCED-
> [EMAIL PROTECTED]] On Behalf Of Stefan Holdermans
> Sent: Friday, June 14, 2002 12:27 PM
> To: [EMAIL PROTECTED]
> Subject: [ADVANCED-DOTNET] Evaluation of boolean expressions.
>
> I really like the way C# handles boolean expressions like:
>
>     a && b
>
> and
>
>     c || d.
>
> If a is false, b is not evaluated, since the expression cannot be
> satisfied
> anyway; if c is true, d is not evaluated.
>
> But -- is this just a Microsoft-specific implementation of the && and
||
> operators? Or do the specifications demand all C# implementations to
> handle
> boolean expressions like this?
>
> E.g., is it safe to code:
>
>     void foo(string bar) {
>         if (bar != null && bar.Length > 3) {
>             ...
>         } else {
>             ...
>         }
>     }
>
> or would a C# implementation be allowed to throw me a
> NullReferenceException if bar is a null reference?
>
> (VB6 would throw you an error, since it actually does always evaluate
the
> second subexpression, indifferent from what the first subexpression
> evaluated to. One can argue that the second subexpression should
always be
> evaluates since it may be a method that, besides returning a boolean,
> incorparates some side-effects... but arent't those side-effects bad
> practice anyway ;)?)
>
> --Stefan

You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced 
DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to