difference between + and & operator

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

    difference between + and & operator

    Hi All,


    is there any performance difference between + and & operator
    while concating string litrels.

    which one is better and why??


    Thanx

    Regards,
    Anoj Kumar

  • _AnonCoward

    #2
    Re: difference between + and & operator


    "Anoj" <sutradhaar@gma il.com> wrote in message
    news:1118206022 .990642.300780@ g44g2000cwa.goo glegroups.com.. .
    :
    : Hi All,
    :
    :
    : is there any performance difference between + and &
    : operator while concating string litrels.
    :
    : which one is better and why??
    :
    :
    : Thanx
    :
    : Regards,
    : Anoj Kumar


    I'm not sure if there is a performance difference or not (probably not)
    but + is ambiguous as it can be used to concatenate strings or add
    numbers. Consider:

    console.writeli ne(3 + 2)
    console.writeli ne(3 & 2)

    The first line will output 5 to the console whereas the second line will
    output 32.

    Use & for concatentations and + for additions.

    Ralf


    Comment

    • Andrew D. Newbould

      #3
      Re: difference between + and &amp; operator

      In message <ldvpe.532$ax3. 47847@twister.s outheast.rr.com >, _AnonCoward
      <abc@xyz.com> writes[color=blue]
      >
      >"Anoj" <sutradhaar@gma il.com> wrote in message
      >news:111820602 2.990642.300780 @g44g2000cwa.go oglegroups.com. ..
      >:
      >: Hi All,
      >:
      >:
      >: is there any performance difference between + and &
      >: operator while concating string litrels.
      >:
      >: which one is better and why??
      >:
      >:
      >: Thanx
      >:
      >: Regards,
      >: Anoj Kumar
      >
      >
      >I'm not sure if there is a performance difference or not (probably not)
      >but + is ambiguous as it can be used to concatenate strings or add
      >numbers. Consider:
      >
      > console.writeli ne(3 + 2)
      > console.writeli ne(3 & 2)
      >
      >The first line will output 5 to the console whereas the second line will
      >output 32.
      >
      >Use & for concatentations and + for additions.
      >
      >Ralf[/color]

      Following on from Ralf, you need to be careful when concatenating
      different data types with + as you can generate curious errors when you
      attempt to ADD a string and integer together (for example) without first
      casting the integer to a string (ie: Cstr() or other .Net method).

      --
      Andrew D. Newbould E-Mail: newsgroups@NOSP AMzadsoft.com

      ZAD Software Systems Web : www.zadsoft.com

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: difference between + and &amp; operator

        "Anoj" <sutradhaar@gma il.com> schrieb:[color=blue]
        > is there any performance difference between + and & operator
        > while concating string litrels.[/color]

        When concatenating strings, the '&' operator is recommended.

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://classicvb.org/petition/>

        Comment

        • Cor Ligthert

          #5
          Re: difference between + and &amp; operator

          Herfried,[color=blue]
          >
          > When concatenating strings, the '&' operator is recommended.
          >[/color]
          I don't agree with you, I would keep it on the answers from Ralf and Andrew

          Just to let Anoj know my opinion, although he is probably not interested in
          that.

          Cor


          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: difference between + and &amp; operator

            "Cor Ligthert" <notmyfirstname @planet.nl> schrieb:[color=blue][color=green]
            >> When concatenating strings, the '&' operator is recommended.
            >>[/color]
            > I don't agree with you, I would keep it on the answers from Ralf and
            > Andrew[/color]

            Huh?!

            <URL:http://msdn.microsoft. com/library/en-us/vbcn7/html/vaconConcatenat ionOperators.as p>:

            | The '&' operator is recommended for string concatenation because it
            | is defined exclusively for strings and reduces your chances of generating
            | an unintended conversion.

            --
            M S Herfried K. Wagner
            M V P <URL:http://dotnet.mvps.org/>
            V B <URL:http://classicvb.org/petition/>

            Comment

            • Cor Ligthert

              #7
              Re: difference between + and &amp; operator

              > "Cor Ligthert" <notmyfirstname @planet.nl> schrieb:[color=blue][color=green][color=darkred]
              >>> When concatenating strings, the '&' operator is recommended.
              >>>[/color]
              >> I don't agree with you, I would keep it on the answers from Ralf and
              >> Andrew[/color]
              >
              > Huh?!
              >
              > | The '&' operator is recommended for string concatenation because it
              > | is defined exclusively for strings and reduces your chances of
              > generating
              > | an unintended conversion.
              >[/color]
              Exactly that we know all. However in this newsgroup is our expirience in my
              opinion more like the answer from Ralf and Andrew.

              When you disagree with me about that, you are free. I found it better to
              tell my opinion about that because of the asked question.

              Non unlikely had Anoj read this on MSDN himself already and asked this here
              to get a better opinion.

              I hope that I am free to disagree with you about this. You are for me free
              to disagree with me.

              When you don't remember anymore why that is. That is because that an integer
              in a concationation can be threaded as a string with Option Strict Off.

              :-)

              Cor


              Comment

              • Andrew D. Newbould

                #8
                Re: difference between + and &amp; operator

                In message <u#wWB2BbFHA.14 04@TK2MSFTNGP09 .phx.gbl>, Cor Ligthert
                <notmyfirstname @planet.nl> writes[color=blue][color=green]
                >> "Cor Ligthert" <notmyfirstname @planet.nl> schrieb:[color=darkred]
                >>>> When concatenating strings, the '&' operator is recommended.
                >>>>
                >>> I don't agree with you, I would keep it on the answers from Ralf and
                >>> Andrew[/color]
                >>
                >> Huh?!
                >>
                >> | The '&' operator is recommended for string concatenation because it
                >> | is defined exclusively for strings and reduces your chances of
                >> generating
                >> | an unintended conversion.
                >>[/color]
                >Exactly that we know all. However in this newsgroup is our expirience in my
                >opinion more like the answer from Ralf and Andrew.
                >
                >When you disagree with me about that, you are free. I found it better to
                >tell my opinion about that because of the asked question.
                >
                >Non unlikely had Anoj read this on MSDN himself already and asked this here
                >to get a better opinion.
                >
                >I hope that I am free to disagree with you about this. You are for me free
                >to disagree with me.
                >
                >When you don't remember anymore why that is. That is because that an integer
                >in a concationation can be threaded as a string with Option Strict Off.
                >[/color]

                Nicely put Cor.

                Another good reason for using + over & is that is makes the developer
                think about who and what the client may do with their application
                forcing them to cast variables properly and stops them getting lazy.
                It's just like learning to deal with NULL's from a database. These
                things should come out during the testing phase.

                Personally. I prefer all my developers to use + over & any day. I'd
                rather the application be a little larger and work rather than slim and
                full of potential disasters :-)

                After all, just because MS thinks its right does not mean it is in
                reality :-)

                --
                Andrew D. Newbould E-Mail: newsgroups@NOSP AMzadsoft.com

                ZAD Software Systems Web : www.zadsoft.com

                Comment

                • Jay B. Harlow [MVP - Outlook]

                  #9
                  Re: difference between + and &amp; operator

                  Anoj,
                  In addition to the other comments:

                  For string *literals* there would be no performance difference as both will
                  do the concatenating at compile time.

                  For string *variables* there would be no performance difference as both call
                  String.Concat to do the work.

                  //000219: Dim s1, s2, r As String
                  //000220: r = s1 & s2
                  IL_0001: ldloc.s s1
                  IL_0003: ldloc.s s2
                  IL_0005: call string [mscorlib]System.String:: Concat(string,
                  string)
                  IL_000a: stloc.s r
                  //000221: r = s1 + s2
                  IL_000c: ldloc.s s1
                  IL_000e: ldloc.s s2
                  IL_0010: call string [mscorlib]System.String:: Concat(string,
                  string)
                  IL_0015: stloc.s r

                  I normally use & as its the String Concatenation Operator, while + is the
                  Addition Operator. As the others have pointed out, + may attempt to add the
                  numerics, however Option Strict On will normally identify an implicit
                  conversion problem...


                  Hope this helps
                  Jay

                  "Anoj" <sutradhaar@gma il.com> wrote in message
                  news:1118206022 .990642.300780@ g44g2000cwa.goo glegroups.com.. .
                  | Hi All,
                  |
                  |
                  | is there any performance difference between + and & operator
                  | while concating string litrels.
                  |
                  | which one is better and why??
                  |
                  |
                  | Thanx
                  |
                  | Regards,
                  | Anoj Kumar
                  |


                  Comment

                  • Herfried K. Wagner [MVP]

                    #10
                    Re: difference between + and &amp; operator

                    "Andrew D. Newbould" <newsgroups@NOz adSPANsoft.com> schrieb:[color=blue]
                    > Another good reason for using + over & is that is makes the developer
                    > think about who and what the client may do with their application forcing
                    > them to cast variables properly and stops them getting lazy. It's just
                    > like learning to deal with NULL's from a database. These things should
                    > come out during the testing phase.
                    >
                    > Personally. I prefer all my developers to use + over & any day. I'd rather
                    > the application be a little larger and work rather than slim and full of
                    > potential disasters :-)[/color]

                    I don't see how using '&' would introduce "potential disasters". Using '&'
                    will perform the type conversions automatically without a need to explicitly
                    convert every operand to a string. This will make code shorter, more
                    self-documenting, easier to edit, understand, and maintain than using '+'
                    and explicit casts.

                    --
                    M S Herfried K. Wagner
                    M V P <URL:http://dotnet.mvps.org/>
                    V B <URL:http://classicvb.org/petition/>

                    Comment

                    Working...