Question: What's the fastest/least resource costing move when concatenating string together from things like datatables? For example:

```
     for (int i = 0; i < darray.Length; i++)
            {
                str_orderbycity += darray[i]["Sort_Order"].ToString() + ",";
                if (i < (darray.Length - 1))
                {
                    str_orderbycity += ",";
                }
            }

```

or waiting till the end and then running the remove method on the string:

```
      str_orderbycity = str_orderbycity.Remove(str_orderbycity.Length - 1, 1);

```

I'm just curious...

Thanks

Answer: 
A `StringBuilder` object, surely?

e.g.

```
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < darray.Length; i++)
        {
            sb.Append(darray[i]["Sort_Order"].ToString());
            sb.Append(",");
        }

        if (darray.Length > 0)
            sb.Remove(sb.Length - 1, 1);

```

It's sole function is to build strings. See [StringBuilder](http://msdn.microsoft.com/en-us/library/system.text.stringbuilder%28v=vs.110%29.aspx)

And, I believe, it's better to test once after the loop, and then remove.
When you use concatenation such as 

```
str_orderbycity += ",";

```

A new string is created (if not already existing in [string intern pool](http://brainspeex.wordpress.com/2011/10/30/string-pooling-in-net-c/)).

[StringBuffer](http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx) avoids those intermediate strings by using maintaining `char array` only.

`StringBuffer` has its costs/overheads so it is not always efficient than `String` concatenation.

From [String Builder Performance consideration - MSDN](http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx)

> 
> The performance of a concatenation operation for a String or StringBuilder object depends on the frequency of memory allocations.  
> 
>  A String concatenation operation always allocates memory, whereas a StringBuilder concatenation operation allocates memory only if the StringBuilder object buffer is too small to accommodate the new data.  
> 
>  Use the String class if you are concatenating a fixed number of String objects. In that case, the compiler may even combine individual concatenation operations into a single operation.  
> 
>  Use a StringBuilder object if you are concatenating an arbitrary number of strings; for example, if you're using a loop to concatenate a random number of strings of user input.
> 
> 
>