tags:

views:

153

answers:

3

My static method returns the following concatenated string like this

return (Sb.ToString() + " " + ds.Tables[1].Rows[0].ItemArray[0].ToString() + " " + ds.Tables[2].Rows[0].ItemArray[0].ToString());

Is this a good/bad practise or should i use stringbuilder for it....

+10  A: 

String concatenation in a single shot will be faster than using a StringBuilder - although if Sb is already a StringBuilder, it might make sense to append to that instead (assuming it's a local variable). Assuming this is actually data which has come from a database, the time taken to fetch it is going to vastly exceed the string concatenation here anyway.

Note that you don't need all these calls to ToString() - this would do just as well:

return (Sb + " " + ds.Tables[1].Rows[0].ItemArray[0] + " " + 
        ds.Tables[2].Rows[0].ItemArray[0]);

Here's the equivalent using the existing builder:

return Sb.Append(" ")
         .Append(ds.Tables[1].Rows[0].ItemArray[0])
         .Append(" ")
         .Append(ds.Tables[2].Rows[0].ItemArray[0])
         .ToString();

This might be slightly faster - it will depend on various things. Personally I'd probably use the concatenation version anyway, as it's slightly simpler IMO. I highly doubt that you'd see much difference in performance. Note that this is a bit of a special case, as you've already got a StringBuilder; in the general case of concatenating a set of items where they can all be specified in one compile-time expression, concatenation can be faster as a single method call can provide all the required information.

I have a page on StringBuilder vs string concatenation if you want more details and guidelines.

Jon Skeet
Will it really be faster? Might be "best practice" to use StringBuilder though, don't you think?
Filip Ekberg
@jon ya `sb` is indeed a stringbuilder...
Pandiya Chendur
@Filip: Yes, it will be faster. It will result in a single call to `String.Concat` which can look at the lengths of *all* the strings involved, work out the exact size of buffer required, copy them all in, and you're done. No extra `StringBuilder` object required; no expanding buffer (potentially involving more reallocations). It's definitely *not* best practice to use `StringBuilder` without understanding why it's *sometimes* more efficient.
Jon Skeet
@Pandiya Chendur: In that case it *may* be faster to append to the existing `StringBuilder` and call `ToString` at the end. I'll edit the answer.
Jon Skeet
@jon great explanation in ur article...
Pandiya Chendur
@Jon, Allright, thanks for clearing that up.
Filip Ekberg
Good article Jon
Sri Kumar
+2  A: 

If you're calling this in a tight loop, you'll be probably be better off using StringBuilder (but this piece should be profiled nevertheless). Otherwise, concatenation is perfectly fine. I would change it, however, to String.Format() for maintainability/readability reasons.

Anton Gogolev
Even if you're calling it in a loop, a `StringBuilder` isn't going to help - notice that this is a *return* statement... the result of the concatenation is going to be returned immediately. `StringBuilder` will almost certainly be *slower* (to an almost irrelevant extent, probably) than using concatenation here.
Jon Skeet
+4  A: 

I think this is more readable:

return String.Format("{0} {1} {2}", Sb, ds.Tables[1].Rows[0].ItemArray[0], ds.Tables[2].Rows[0].ItemArray[0]);

About concatenating strings or using StringBuilder:

Concatenating Strings Efficiently
StringBuilder and String Concatenation

tsocks