Home All Groups Group Topic Archive Search About

Concatenation - String.Concat

Author
6 Feb 2006 5:42 PM
Mythran
Out of curiosity, only, which is recommended for SHORT concatenation...or
concatenating two or three strings that are relatively small in size?

Dim a As String = "bah"
Dim b As String = "bah2"
Dim c As String = a & b
Dim d As String = String.Concat(a, b)

string a = "bah";
string b = "bah2";
string c = a + b;
string d = string.Concat(a, b);

Like I said, doesn't really matter .. but sometimes I use one, and other
times I use the other.  Is there a recommended one?  Should I use the
concatenation operator (+/&) for small, known strings and Concat method when
the size/number of strings is unknown?

Thanks,
Mythran

Author
6 Feb 2006 5:48 PM
Jon Skeet [C# MVP]
Mythran <kip_potter@hotmail.comREMOVETRAIL> wrote:
Show quoteHide quote
> Out of curiosity, only, which is recommended for SHORT concatenation...or
> concatenating two or three strings that are relatively small in size?
>
> Dim a As String = "bah"
> Dim b As String = "bah2"
> Dim c As String = a & b
> Dim d As String = String.Concat(a, b)
>
> string a = "bah";
> string b = "bah2";
> string c = a + b;
> string d = string.Concat(a, b);
>
> Like I said, doesn't really matter .. but sometimes I use one, and other
> times I use the other.  Is there a recommended one?  Should I use the
> concatenation operator (+/&) for small, known strings and Concat method when
> the size/number of strings is unknown?

They compile to the same code - the compiler uses String.Concat
internally. However, I believe that using the operator is usually more
readable than using string.Concat explicitly.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
6 Feb 2006 5:52 PM
Mythran
Show quote Hide quote
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MPG.1e519c637d669ac398cddb@msnews.microsoft.com...
> Mythran <kip_potter@hotmail.comREMOVETRAIL> wrote:
>> Out of curiosity, only, which is recommended for SHORT concatenation...or
>> concatenating two or three strings that are relatively small in size?
>>
>> Dim a As String = "bah"
>> Dim b As String = "bah2"
>> Dim c As String = a & b
>> Dim d As String = String.Concat(a, b)
>>
>> string a = "bah";
>> string b = "bah2";
>> string c = a + b;
>> string d = string.Concat(a, b);
>>
>> Like I said, doesn't really matter .. but sometimes I use one, and other
>> times I use the other.  Is there a recommended one?  Should I use the
>> concatenation operator (+/&) for small, known strings and Concat method
>> when
>> the size/number of strings is unknown?
>
> They compile to the same code - the compiler uses String.Concat
> internally. However, I believe that using the operator is usually more
> readable than using string.Concat explicitly.
>
> --
> Jon Skeet - <sk***@pobox.com>
> http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too

Ok, thanks Jon.

Mythran
Author
6 Feb 2006 5:51 PM
Herfried K. Wagner [MVP]
Show quote Hide quote
"Mythran" <kip_potter@hotmail.comREMOVETRAIL> schrieb:
> Out of curiosity, only, which is recommended for SHORT concatenation...or
> concatenating two or three strings that are relatively small in size?
>
> Dim a As String = "bah"
> Dim b As String = "bah2"
> Dim c As String = a & b
> Dim d As String = String.Concat(a, b)
>
> string a = "bah";
> string b = "bah2";
> string c = a + b;
> string d = string.Concat(a, b);
>
> Like I said, doesn't really matter .. but sometimes I use one, and other
> times I use the other.  Is there a recommended one?  Should I use the
> concatenation operator (+/&) for small, known strings and Concat method
> when

I suggest to use the '&' operator in the examples described above.  This
will enable the compiler to concatenate string literals at compile time in
some cases such as '"Bla" & ControlChars.NewLine & "Goo"'.

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