C# representation of VB.net "OrElse" operator?

R

Rich P

VB.net:
If this Or that Then...

C#
if (this || that){...}

VB.net If this OrElse that Then

C#
if (this .....?

I believe in VB.Net OrElse wont evaluate the next expression unless the
first expression is completely false where with OR both expressions get
evaluated - I think - I never get this part straight. Does C# have an
equivalent to OrElse? Or is it just a straight forward || for
everything that is Or/OrElse related?


Rich
 
J

Jeff Johnson

VB.net:
If this Or that Then...

C#
if (this || that){...}

VB.net If this OrElse that Then

C#
if (this .....?

I believe in VB.Net OrElse wont evaluate the next expression unless the
first expression is completely false where with OR both expressions get
evaluated - I think - I never get this part straight. Does C# have an
equivalent to OrElse? Or is it just a straight forward || for
everything that is Or/OrElse related?

|| = OrElse. C# has NO equivalent to VB's OR statement as far as logical
comparisons go. In other words, you can't make C# NOT short-circuit. To do
so would require multiple if statements.

Bitwise, VB's OR = C#'s |.
 
J

Jeff Johnson

|| = OrElse. C# has NO equivalent to VB's OR statement as far as logical
comparisons go. In other words, you can't make C# NOT short-circuit. To do
so would require multiple if statements.

Bitwise, VB's OR = C#'s |.

I should expound. Normally you ALWAYS want short-circuiting. The only time
you wouldn't would be if the conditional had side effects and you wanted to
be sure to trigger those side effects for all conditionals. I think most
folks in this group would say that it's a bad idea to put all that into an
if statement and would instead recommend that you execute the functions to
produce those side effects first and capture the results, thne make your
conditional test the results.

In other words, I can't imagine why anyone would try to duplicate VB's
logical OR behavior. It was dumb to begin with (and this is coming from a VB
guy!) and I was thrilled to see VB "grow up" and FINALLY get a
short-circuiting operator. The few times I write in VB.NET (2005+) I always
use OrElse.
 
K

keremskusmezer

I should expound. Normally you ALWAYS want short-circuiting. The only time
you wouldn't would be if the conditional had side effects and you wanted to
be sure to trigger those side effects for all conditionals. I think most
folks in this group would say that it's a bad idea to put all that into an
if statement and would instead recommend that you execute the functions to
produce those side effects first and capture the results, thne make your
conditional test the results.

In other words, I can't imagine why anyone would try to duplicate VB's
logical OR behavior. It was dumb to begin with (and this is coming from aVB
guy!) and I was thrilled to see VB "grow up" and FINALLY get a
short-circuiting operator. The few times I write in VB.NET (2005+) I always
use OrElse.

Yes, it was fun when i switched my code from C# to VB.Net First Time.

if ( someStringObject != null && someStringObject.Length > 0)

NullReferenceException bada :)
if someStringObject is nothing and someStringObject.Length = 0 then

end if
 
C

CY

if ( someStringObject != null && someStringObject.Length > 0)

NullReferenceException bada :)
if someStringObject is nothing and someStringObject.Length = 0 then

end if- Dölj citerad text -

- Visa citerad text -

You did that or the "helper", havent tried anything C# but VB6->VB.NET
and it went ... lets just say it stays in VB6 for a while... still
cant sleep after the first chock... ;)

//CY
 
F

Family Tree Mike

Rich P said:
VB.net:
If this Or that Then...

C#
if (this || that){...}

VB.net If this OrElse that Then

C#
if (this .....?

I believe in VB.Net OrElse wont evaluate the next expression unless the
first expression is completely false where with OR both expressions get
evaluated - I think - I never get this part straight. Does C# have an
equivalent to OrElse? Or is it just a straight forward || for
everything that is Or/OrElse related?


Rich


One bar tests both sides of the conditional, while two bars skips out if the
first is true.
 
J

Jeff Johnson

One bar tests both sides of the conditional, while two bars skips out if
the first is true.

Hmmm, now that I think about it, that WILL work, but I guess since I see " |
" as purely a bitwise operator I didn't consider this route. But won't the
compiler throw a warning? Ultimately, you normally SHOULDN'T do this, even
if you CAN....
 
F

Family Tree Mike

Jeff Johnson said:
Hmmm, now that I think about it, that WILL work, but I guess since I see "
| " as purely a bitwise operator I didn't consider this route. But won't
the compiler throw a warning? Ultimately, you normally SHOULDN'T do this,
even if you CAN....


No, they are specifically allowed in the language (one bar, I mean).
& and && are also both defined for conditionals, but that is another
question...
 
G

Göran Andersson

Jeff said:
|| = OrElse. C# has NO equivalent to VB's OR statement as far as logical
comparisons go. In other words, you can't make C# NOT short-circuit. To do
so would require multiple if statements.

That's not correct. The | operator does that.

It's not very well known, and rarely used. Although I have known about
it for quite some time, I never had any use for it until a few days ago...
Bitwise, VB's OR = C#'s |.

Only for integer operands.
 
J

Jeff Johnson

That's not correct. The | operator does that.

It's not very well known, and rarely used. Although I have known about it
for quite some time, I never had any use for it until a few days ago...

So then this is a case where C# differs from C, right? Because I'm pretty
sure a C compiler would complain (warning) if it thought you used | when you
meant ||.
 
G

Göran Andersson

Jeff said:
So then this is a case where C# differs from C, right? Because I'm pretty
sure a C compiler would complain (warning) if it thought you used | when you
meant ||.

Yes, in C the | and & operators are only binary operators, they are not
defined for logical operands. That makes a bit of sense, it would be
rather a mess as C has implicit conversion between integers and boolean.

How C# distinguishes between types is actually more like Pascal (Delphi)
than C.
 
B

Ben Voigt [C++ MVP]

Family said:
No, they are specifically allowed in the language (one bar, I mean).

Which is to say that the language defines "||" as nothing more than "the
short-circuiting version of |"
 
J

J.B. Moreno

Göran Andersson said:
Yes, in C the | and & operators are only binary operators, they are not
defined for logical operands. That makes a bit of sense, it would be
rather a mess as C has implicit conversion between integers and boolean.

Actually, no. In C the result of the bitwise operation is compared to
zero, and if not equal it's considered true.

(Well, it might give a warning depending upon the specific compiler and
it's settings but it'd work).
 

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

Top