Concatenation - String.Concat

M

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
 
J

Jon Skeet [C# MVP]

Mythran said:
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.
 
H

Herfried K. Wagner [MVP]

Mythran said:
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

Mythran

Jon Skeet said:
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.

Ok, thanks Jon.

Mythran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top