Conditionals with | vs ||

  • Thread starter Thread starter DotNetGruven
  • Start date Start date
D

DotNetGruven

Is there ever any reason to do this:

if (messageString == null | messageString.Length==0)

instead of this

if ( messageString == null || messageString.Length == 0 )

??

TIA

geo
 
As I understand it, the first statement causes both sides of the OR to be
evaluated; if messageString is null, the second expression will throw an
exception. In your second statement, if the left side evaluates as true, the
second expression won't be evaluated and consequently won't throw the
exception. This is called, I think, "lazy evaluation", and your example is
the textbook case for why you would use the feature.

Tom Dacon
Dacon Software Consulting
 
| is bitwise OR of its operands while || is a logical-OR of its bool
operands

1) | can applied for the integral types and bool while || can only be
applied to bool
2) | or || make no difference in result when both operands are bool
 
Hi,
| may have two meanings, if used with bool it perform a logic or but it
always evaluate the second operand, no matter what the first result. This
can be used for example if the second operand has any side-effect.
When the | is used with integers it perform a bitwise OR.

|| is a logic OR but it only evaluate the second operand if needed, if the
first one is true it does not perform the extra operation.


Cheers,
 
Jiachuan Wang said:
| is bitwise OR of its operands while || is a logical-OR of its bool
operands

1) | can applied for the integral types and bool while || can only be
applied to bool
2) | or || make no difference in result when both operands are bool

Yes it does - as others have pointed out, || uses short-circuiting. Try
this:

using System;

class Test
{
static void Main()
{
Console.WriteLine ("Using ||");
bool x=PrintHelloAndReturnTrue() || PrintHelloAndReturnTrue();
Console.WriteLine ("Using |");
bool y=PrintHelloAndReturnTrue() | PrintHelloAndReturnTrue();
}

static bool PrintHelloAndReturnTrue()
{
Console.WriteLine("Hello");
return true;
}
}
 
Jiachuan Wang said:
| is bitwise OR of its operands while || is a logical-OR of its bool
operands

1) | can applied for the integral types and bool while || can only be
applied to bool
2) | or || make no difference in result when both operands are bool

Yes it does and a lot !!!

SomeRefType instance = null ;

This will runs fine , as it has short circuit the second operand will not
be evaluated

if ( (instance != null) || (instance.SomePRoperty) )

if you run the same line with | you will get a NullReferenceException

Another thing you have to take in mind is the side effect of the second
operand ( as the example Jon posted )


Cheers,
 
Hi Geo,

Does the community's reply make sense to you? Do you still have any concern
on this issue?

Please feel free to post. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top