String.Concat or +

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I know that for XML or large concatenations StringBuilder is
better ...

But for joining 3 or 4 strings like:
Encoding.UTF8.GetBytes(user.Username + user.Email +
user.Created.ToString());

Should I use + or String.Concat?

Thanks,
Miguel
 
Hello,

I know that for XML or large concatenations StringBuilder is
better ...

But for joining 3 or 4 strings like:
Encoding.UTF8.GetBytes(user.Username + user.Email +
user.Created.ToString());

Should I use + or String.Concat?

I'm guessing you'll get mostly opinionated reponses. I would
personally use the + operator.
 
Hello,

I know that for XML or large concatenations StringBuilder is
better ...

But for joining 3 or 4 strings like:
Encoding.UTF8.GetBytes(user.Username + user.Email +
user.Created.ToString());

Should I use + or String.Concat?

The two are equivalent. I see no reason to prefer one over the other,
except stylistically (and thus subjectively).

I suppose most people would prefer the + operator, due to its
conciseness. But if you prefer to type more, String.Concat() is fine too.

Pete
 
The two are equivalent.  I see no reason to prefer one over the other,  
except stylistically (and thus subjectively).

I suppose most people would prefer the + operator, due to its  
conciseness.  But if you prefer to type more, String.Concat() is fine too.

Pete

I was making the question because I see both in C# examples so I was
wondering which one would be better.

Thank You Pete
 
shapper said:
Hello,

I know that for XML or large concatenations StringBuilder is
better ...

But for joining 3 or 4 strings like:
Encoding.UTF8.GetBytes(user.Username + user.Email +
user.Created.ToString());

Should I use + or String.Concat?

Thanks,
Miguel

That's entirely up to your taste. The generated code will be completely
identical whichever you use.

This expression:

user.UserName + user.Email + user.Created.ToString()

actually compiles into:

String.Concat(user.UserName, user.Email, user.Created.ToString())
 
shapper said:
Hello,

I know that for XML or large concatenations StringBuilder is
better ...

Not really. For concatenation inside a loop (or callback, or whatever)
where the items are not all known before concatenation begins, StringBuilder
is better. As long as the length of each piece is available up front,
String.Concat is optimal.
But for joining 3 or 4 strings like:
Encoding.UTF8.GetBytes(user.Username + user.Email +
user.Created.ToString());

Should I use + or String.Concat?

Like Göran said, they both result in a call to String.Concat (at least when
used outside of C# 4.0 "dynamic" context).
 

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

Similar Threads

Create Key ... Need Advice 5
Token Expiration Date. How? 11
Approve User 1
String 3
encrypt / decrypt string with SHA512 2
Base64 and Utf8. What am I missing? 4
Concatenation - String.Concat 3
XML ID ... 7

Back
Top