tags:

views:

340

answers:

7

Hi

I been wondering this for a while. Why use String.Concat() instead of using the plus operator. I understand the String.Format since it a voids using the plus operator and make your code looker nicer.

like for example

string one = "bob";
string two = "jim";

string three = one + two;
string three = String.Concat(one,two);
+1  A: 

Because you can use the version that takes two objects ;)

tster
Why is this important? Can't you just do .toString()?
chobo2
@chobo2, That's done implicitly if you call `String.Concat()`, so you don't need to worry about the types of the objects.
strager
@chobo2, you need to account for null values as well!
tster
So it converts "nulls" to strings and .toString() wont? Won't you still have to test for null? Like if your planning to use it somewhere to do something won't it be better to test for null? If thats the case can't you test it before you convert it to the string?
chobo2
`string.Concat(foo, bar);` will work OK with nulls. `foo.ToString() + bar.ToString()` will cause a NullReferenceException.
tster
A: 

To be honest; some people prefer it. I know I would rather use Concat as the + really isn't very intuitive. Especially just looking at the code deep down; it's hard to tell if those are numbers you're adding or two strings your concating or two points that you're doing vector addition with. :)

Suroot
If it's hard to tell what it is that you are adding, that is rather a problem with variable naming than the use of the + operator. Of course if the variable names suck, the String.Concat call will clear up some things.
Guffa
I agree. If you can't tell that two strings are being concatenated that is a serious issue with the code or the programmer.
Ed Swangren
+13  A: 

As long as the two operands are strings, there is no difference at all between using the + operator and the String.Concat method. The code using the + operator actually compiles into a String.Concat call.

Use the one that best represents your intention with the code.

Guffa
What you mean "best represents your intention with the code"?
chobo2
Whichever is more readable to you and your dev team.One nice thing about String.Concat is that it's easier to *not* just keep adding strings together inline which isn't quite as performant as usign a stringbuilder.
Paul
A: 

I believe it’s the same as string a + string b. String type in C# being immutable therefore the preferred way of string manipulation is through StringBuilder. As Guffa has concluded "Use the one that best represents your intention with the code." Of course for simple usage where performance isn't too much of a concern use + or concat.

Jeffrey C
A: 

Portability (yours).

In other languages, + might not concatenate as you expect when presented with two strings. It might do something altogether unexpected. Or, in some loosely typed languages if you pit the + between an integer or numeric scalar variable and a string, it might not throw a compiler warning, try to convert the strung to a numeric, and add them.

In comparison, .concat() is quite obvious and readable compared to + in some cases.

Kyle Hodgson
On the other hand, if you're going to program in a language you should really have some clue about that language, rather than trying to only stick to things which will look the same everywhere.
Jon Skeet
very true, of course.
Kyle Hodgson
+1  A: 

Hi Guys, I myself have been having this same question and this question triggered me to investigate into it a bit,

I create the following class

public class Class1
{
    string str = "One" + "Team";
    string str2 = string.Concat("One", "Team");
}

And below is corresponding IL code for that.

.method public hidebysig specialname
rtspecialname 
        instance void  .ctor() cil managed {   // Code size       40
(0x28)   .maxstack  8   
IL_0000: ldarg.0   
IL_0001:  ldstr     "OneTeam"   
IL_0006:  stfld     string StringConcat.Class1::str  
IL_000b:  ldarg.0   
IL_000c:  ldstr   "One"   IL_0011:  ldstr      "Team"  
IL_0016:  call       string [mscorlib]System.String::Concat(string, string)   
IL_001b:  stfld      string StringConcat.Class1::str2   
IL_0020: ldarg.0   
IL_0021: call      instance void[mscorlib]System.Object::.ctor()  
IL_0026:  nop   IL_0027:  ret } // end of method Class1::.ctor

For me it definitely looks like string.Concat is having more steps than overloaded + operator. But I know for sure inside the system.string class there will be similer set of operations happening for overloaded + operator as well. Thoughts?

Bumble Bee
You've used two constant strings - so in this particular case the compiler has performed the concatenation for you. Do it with string *variables* and you'll see the C# compiler turn + into a call to String.Concat.
Jon Skeet
A: 

I use + when I know how many strings are going to be concatenated - but what if you just have an array? In that case you wouldn't know how many times to apply +, so you'd have to call a method (or loop yourself, which is horrible).

I can't remember calling string.Concat very often though - it's a definite rarity.

As guffa says, + compiles down into calls to string.Concat anyway - it's worth being aware that string doesn't actually have a + operator, which can be a cause of confusion if you ever try to use it in reflection!

One benefit of + though is that if all the arguments are constant expressions, the compiler will perform the concatenation for you, so you don't need to do it at execution time. That sort of tiny performance benefit isn't going to be significant in most code, but it's always nice when the code I find most readable also has a performance advantage :)

Jon Skeet