How to translate OrElse and AndAlso from VB.NET?

S

Siegfried Heintze

Are there operators in C# that are the counterparts of "OrElse" and
"AndAlso" that I can use when translating the following program from VB.NET
to C#?

Thanks,
Siegfried

Namespace vbtest
Module Main
Function reflect(x as boolean) as boolean
System.Console.Out.WriteLine(" reflect = {0}", x)
reflect = x
End Function
Sub Main(ByVal args() As String)
System.Console.Out.WriteLine(" reflect(true) Or reflect(true) =
{0}", reflect(true) Or reflect(true))
System.Console.Out.WriteLine(" reflect(true) OrElse reflect(true) =
{0}", reflect(true) OrElse reflect(true))
System.Console.Out.WriteLine(" reflect(false) And reflect(false) =
{0}", reflect(false) And reflect(false))
System.Console.Out.WriteLine(" reflect(false) AndAlso reflect(false)
= {0}", reflect(false) AndAlso reflect(false))
End Sub
End Module
End Namespace
 
N

Nicholas Paldino [.NET/C# MVP]

Siegfried,

Yes, && and || respectively.

C#, Java and other C++-based languages have a tradition of
short-circuiting expressions if the outcome can be determined with the first
expression.

For example, if you had a method that returned true, and another that
returned false, and did this:

if (ReturnsTrue() || ReturnsFalse())

In C#, Java, etc, etc, the ReturnsFalse method would never be called, as
the ReturnsTrue method returns true, and anything ORed with true is true, so
there is no need to evaluate the second expression. The same goes for:

if (ReturnsFalse() && ReturnsTrue())

Since false ANDed with anything is false, it doesn't execute
ReturnsTrue.

Now, in VB, all the expressions are ALWAYS evaluated (when using And or
Or), which is why AndAlso and OrElse were added, so that you could have this
short-circuiting.
 
D

David Anton

Actually, there is no difference between VB and C# in this regard.
Both have short-circuiting logical operators (AndAlso/OrElse and &&/||).

Both have non-short-circuiting logical operators, which also serve as
bitwise operators:
And/Or in VB
& and | in C#

The proper use of And/Or and &/| is for bitwise operations, but they can
also be used for (inefficient) logical operations.
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++ VB Edition: VB to C++/CLI
Instant C++ C# Edition: C# to C++/CLI


Nicholas Paldino said:
Siegfried,

Yes, && and || respectively.

C#, Java and other C++-based languages have a tradition of
short-circuiting expressions if the outcome can be determined with the first
expression.

For example, if you had a method that returned true, and another that
returned false, and did this:

if (ReturnsTrue() || ReturnsFalse())

In C#, Java, etc, etc, the ReturnsFalse method would never be called, as
the ReturnsTrue method returns true, and anything ORed with true is true, so
there is no need to evaluate the second expression. The same goes for:

if (ReturnsFalse() && ReturnsTrue())

Since false ANDed with anything is false, it doesn't execute
ReturnsTrue.

Now, in VB, all the expressions are ALWAYS evaluated (when using And or
Or), which is why AndAlso and OrElse were added, so that you could have this
short-circuiting.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Siegfried Heintze said:
Are there operators in C# that are the counterparts of "OrElse" and
"AndAlso" that I can use when translating the following program from
VB.NET to C#?

Thanks,
Siegfried

Namespace vbtest
Module Main
Function reflect(x as boolean) as boolean
System.Console.Out.WriteLine(" reflect = {0}", x)
reflect = x
End Function
Sub Main(ByVal args() As String)
System.Console.Out.WriteLine(" reflect(true) Or reflect(true) =
{0}", reflect(true) Or reflect(true))
System.Console.Out.WriteLine(" reflect(true) OrElse reflect(true) =
{0}", reflect(true) OrElse reflect(true))
System.Console.Out.WriteLine(" reflect(false) And reflect(false) =
{0}", reflect(false) And reflect(false))
System.Console.Out.WriteLine(" reflect(false) AndAlso
reflect(false) = {0}", reflect(false) AndAlso reflect(false))
End Sub
End Module
End Namespace
 

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