views:

121

answers:

2

Possible Duplicate:
The difference between + and & for joining strings in VB.Net

In VB.NET you can use either += or &= to concatenante a string (ignoring using a StringBuilder for this question -- this is directed at a very simple concatenation).

I'm curious to know if and what the differences are of using one or the other in VB.NET.

+5  A: 

None.

As you can see below. These two lines of code compiles exactly to the same IL code:

Module Module1

Sub Main()
    Dim s1 As String = "s1"
    Dim s2 As String = "s2"
    s2 += s1
    s1 &= s2
End Sub

End Module

Compiles to (note System.String::Concat):

.method public static void  Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 ) 
// Code size       31 (0x1f)
.maxstack  2
.locals init ([0] string s1,
       [1] string s2)
IL_0000:  nop
IL_0001:  ldstr      "s1"
IL_0006:  stloc.0
IL_0007:  ldstr      "s2"
IL_000c:  stloc.1
IL_000d:  ldloc.1
IL_000e:  ldloc.0
IL_000f:  call       string [mscorlib]System.String::Concat(string,
                                                          string)
IL_0014:  stloc.1
IL_0015:  ldloc.0
IL_0016:  ldloc.1
IL_0017:  call       string [mscorlib]System.String::Concat(string,
                                                          string)
IL_001c:  stloc.0
IL_001d:  nop
IL_001e:  ret
} // end of method Module1::Main
Aliostad
There is a difference in how they work depending on the types of objects you are adding.
R0MANARMY
The question is specifically on strings.
Aliostad
rickp
No probs. It is always good to have a look at the IL code using ILDASM. Initially it is unfamiliar but gradually you get used to it.
Aliostad