Home All Groups Group Topic Archive Search About

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

Author
23 Jun 2009 4:02 PM
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

*** Sent via Developersdex http://www.developersdex.com ***

Author
23 Jun 2009 4:16 PM
Jeff Johnson
Show quote Hide quote
"Rich P" <rpng***@aol.com> wrote in message
news:eFchAwB9JHA.4176@TK2MSFTNGP02.phx.gbl...

> 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 |.
Are all your drivers up to date? click for free checkup

Author
23 Jun 2009 4:31 PM
Jeff Johnson
Show quote Hide quote
"Jeff Johnson" <i.get@enough.spam> wrote in message
news:OgNn43B9JHA.1376@TK2MSFTNGP02.phx.gbl...

>> 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 |.

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.
Author
24 Jun 2009 8:24 AM
Göran_Andersson
Jeff Johnson wrote:
Show quoteHide quote
> "Rich P" <rpng***@aol.com> wrote in message
> news:eFchAwB9JHA.4176@TK2MSFTNGP02.phx.gbl...
>
>> 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.

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.

--
Göran Andersson
_____
http://www.guffa.com
Author
24 Jun 2009 1:10 PM
Jeff Johnson
"Göran Andersson" <gu***@guffa.com> wrote in message
news:O0OJCVK9JHA.200@TK2MSFTNGP05.phx.gbl...

>> || = 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...

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 ||.
Author
24 Jun 2009 5:00 PM
Göran_Andersson
Jeff Johnson wrote:
Show quoteHide quote
> "Göran Andersson" <gu***@guffa.com> wrote in message
> news:O0OJCVK9JHA.200@TK2MSFTNGP05.phx.gbl...
>
>>> || = 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...
>
> 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.

--
Göran Andersson
_____
http://www.guffa.com
Author
23 Jun 2009 4:18 PM
Heandel
Simple:

if(this)
{

}
else if (that)
{

}
else
{

}

Heandel

"Rich P" <rpng***@aol.com> a écrit dans le message de groupe de discussion :
eFchAwB9JHA.4***@TK2MSFTNGP02.phx.gbl...
Show quoteHide quote
> 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
>
> *** Sent via Developersdex http://www.developersdex.com ***
Author
23 Jun 2009 9:23 PM
Family Tree Mike
Show quote Hide quote
"Rich P" <rpng***@aol.com> wrote in message
news:eFchAwB9JHA.4176@TK2MSFTNGP02.phx.gbl...
> 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
>
> *** Sent via Developersdex http://www.developersdex.com ***


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

--
Mike
Author
23 Jun 2009 9:32 PM
Jeff Johnson
"Family Tree Mike" <FamilyTreeM***@ThisOldHouse.com> wrote in message
news:D1DE9B39-5F84-426F-B897-CF55B963536E@microsoft.com...

> 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....
Author
23 Jun 2009 10:06 PM
Family Tree Mike
Show quote Hide quote
"Jeff Johnson" <i.get@enough.spam> wrote in message
news:O0jevoE9JHA.1248@TK2MSFTNGP04.phx.gbl...
> "Family Tree Mike" <FamilyTreeM***@ThisOldHouse.com> wrote in message
> news:D1DE9B39-5F84-426F-B897-CF55B963536E@microsoft.com...
>
>> 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....
>


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

--
Mike
Author
24 Jun 2009 9:50 PM
Ben Voigt [C++ MVP]
Family Tree Mike wrote:
Show quoteHide quote
> "Jeff Johnson" <i.get@enough.spam> wrote in message
> news:O0jevoE9JHA.1248@TK2MSFTNGP04.phx.gbl...
>> "Family Tree Mike" <FamilyTreeM***@ThisOldHouse.com> wrote in message
>> news:D1DE9B39-5F84-426F-B897-CF55B963536E@microsoft.com...
>>
>>> 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....
>>
>
>
> 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 |"

Bookmark and Share