How to translate OrElse and AndAlso from VB.NET?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Siegfried Heintze

    How to translate OrElse and AndAlso from VB.NET?

    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


  • cfps.Christian

    #2
    Re: How to translate OrElse and AndAlso from VB.NET?

    OrElse = ||
    AndAlso = &&

    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: How to translate OrElse and AndAlso from VB.NET?

      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]
      - mvp@spam.guard. caspershouse.co m

      "Siegfried Heintze" <siegfried@hein tze.comwrote in message
      news:%23lg5huHV IHA.5164@TK2MSF TNGP03.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
      >
      >

      Comment

      • =?Utf-8?B?RGF2aWQgQW50b24=?=

        #4
        Re: How to translate OrElse and AndAlso from VB.NET?

        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.
        --
        Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters

        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 [.NET/C# MVP]" wrote:
        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]
        - mvp@spam.guard. caspershouse.co m
        >
        "Siegfried Heintze" <siegfried@hein tze.comwrote in message
        news:%23lg5huHV IHA.5164@TK2MSF TNGP03.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
        >
        >
        >

        Comment

        Working...