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 ***
> 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 |.
if(this)
{
}
else if (that)
{
}
else
{
}
Heandel
"Rich P" <rpn...@aol.com> a �crit dans le message de groupe de discussion :
eFchAwB9...@TK2MSFTNGP02.phx.gbl...
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.
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
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
One bar tests both sides of the conditional, while two bars skips out if the
first is true.
--
Mike
> 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
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
>> || = 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.
Which is to say that the language defines "||" as nothing more than "the
short-circuiting version of |"
> Jeff Johnson wrote:
> > "G�ran Andersson" <gu...@guffa.com> wrote in message
-snip-
> >> 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.
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).
--
J.B. Moreno