|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
C# representation of VB.net "OrElse" operator?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 ***
Show quote
Hide quote
"Rich P" <rpng***@aol.com> wrote in message comparisons go. In other words, you can't make C# NOT short-circuit. To do 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 so would require multiple if statements. Bitwise, VB's OR = C#'s |.
Show quote
Hide quote
"Jeff Johnson" <i.get@enough.spam> wrote in message I should expound. Normally you ALWAYS want short-circuiting. The only time 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 |. 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. Jeff Johnson wrote:
Show quoteHide quote > "Rich P" <rpng***@aol.com> wrote in message That's not correct. The | operator does that.> 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. 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" <gu***@guffa.com> wrote in message So then this is a case where C# differs from C, right? Because I'm pretty 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... sure a C compiler would complain (warning) if it thought you used | when you meant ||. Jeff Johnson wrote:
Show quoteHide quote > "Göran Andersson" <gu***@guffa.com> wrote in message Yes, in C the | and & operators are only binary operators, they are not > 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 ||. > 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. 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 ***
Show quote
Hide quote
"Rich P" <rpng***@aol.com> wrote in message One bar tests both sides of the conditional, while two bars skips out if the 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 *** first is true. -- Mike "Family Tree Mike" <FamilyTreeM***@ThisOldHouse.com> wrote in message Hmmm, now that I think about it, that WILL work, but I guess since I see " | 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. " 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....
Show quote
Hide quote
"Jeff Johnson" <i.get@enough.spam> wrote in message No, they are specifically allowed in the language (one bar, I mean).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.... > & and && are also both defined for conditionals, but that is another question... -- Mike Family Tree Mike wrote:
Show quoteHide quote > "Jeff Johnson" <i.get@enough.spam> wrote in message Which is to say that the language defines "||" as nothing more than "the > 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). short-circuiting version of |"
Other interesting topics
False Positives From String Comparison using string.Equals()
creating an array of forms Future of CSharp vs future of VB webservice LINQ - retrieve differences between two generic Lists MD5 hashes always unique? MSN Display Picture in C# SqlDependency - MySQL equivalent Register for COM interop 64 bit problem Compress and decompress with c#? |
|||||||||||||||||||||||