Short-circuit evaluation in C# and VB.NET

In my early years of college as a CS student I learned about short-circuit evaluation.  

Short-circuit evaluation denotes that the second argument is only evaluated if the first argument does not suffice the value of the expression. I found out that short-circuit evaluation in C# is different from VB.NET. Consider the following codes.

Here, if MyFunctionA() returns false and MyFunction() will not be executed.

if (MyFunctionA() && MyFunctionB())
{              

}

Same with this, if MyFunctionA() returns true, MyFunctionB() will not be evaluated.

if (MyFunctionA() || MyFunctionB())
{
 
//Some codes here         
}

In VB.NET, it’s different. Even if MyFunctionA() returns false, MyFunctionB() will also evaluated. There’s no short-circuit evaluation happened.

If (MyFunctionA() And MyFunctionB()) Then
 'Some codes here

End If

 
Same with this,

If (MyFunctionA() Or MyFunctionB()) Then
 'Some codes here

End If

 
Here’s the catch, to be able to achieve the short-circuit evalution in VB.NET use
AndAlso and OrElse keyword.

If (MyFunctionA() AndAlso MyFunctionB()) Then

If (MyFunctionA() OrElse MyFunctionB()) Then

 

I wonder why the keywords (instead of using And only and Or for VB.NET) are different from these 2 languages considering they were both from MS....Confused

Published 09-28-2007 3:46 PM by n.ocampo
Filed under: , ,

Comments

# re: Short-circuit evaluation in C# and VB.NET

Friday, September 28, 2007 1:18 AM by cruizer

not sure, but it could be that And and Or did not perform short-circuit eval on old VB (not .NET). so they did this for backward compatibility of sorts.

# re: Short-circuit evaluation in C# and VB.NET

Friday, September 28, 2007 1:59 AM by n.ocampo

Backward compatibility? yeah, it might be. But it would be more cleaner if they did "And" and "Or" be short-circuit evaluated, instead of introducing the "AndAlso" and "OrElse" keyword.

# re: Short-circuit evaluation in C# and VB.NET

Friday, September 28, 2007 3:26 AM by cruizer

but really, who uses VB.NET? :P I'll only recommend it to those from a VB6 background, and even these are one of the few stuff that's really backward compatible between VB.NET and VB6...

if you're more familiar with C and its short-circuiting behavior, you'll be much better off with C# instead.

# re: Short-circuit evaluation in C# and VB.NET

Friday, September 28, 2007 11:35 AM by keithrull

@cruizer: hey hey hey hey! i still use VB.NET :P I think VB won't go out anytime soon. I still use it whenever my client preffers it over curly brackets and i don't mind it either.

Here's a quick overview of how to accomplish AndAlso and OrElse in C#

www.c-sharpcorner.com/.../AndAlsoOrElseOperatorsinCSharp.aspx

# re: Short-circuit evaluation in C# and VB.NET

Sunday, September 30, 2007 7:11 PM by n.ocampo

@cruizer: I also recommend VB.NET to those from VB6. I've came from that path also. But after switching from VB.NET to C# last year. I would say that I'm lovin' it. :P

@keith: same here. I only use VB.NET if our client prefers it.

# re: Short-circuit evaluation in C# and VB.NET

Thursday, May 29, 2008 10:56 PM by Euo

That's why I loved c# more now than vb.net and hoping that my future clients will not require me to write in vb.net again. :D

# re: Short-circuit evaluation in C# and VB.NET

Monday, August 25, 2008 10:11 AM by Lightsabre

Get over the vb.net vs c#.  There are hundreds, if not thousands of langauages with different sytnax and functionality.  Every client is different and not every project 'allows' you to use your language of preference.  Therefore get to know the ones in your area and 'know' the differences.