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]
-
mv*@spam.guard.caspershouse.com
"Siegfried Heintze" <si*******@heintze.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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