I\'ve been tinkering with small functions on my own time, trying to find ways to refactor them (I recently read Martin Fowler\'s book Refactoring: Improving the Design of Ex
This is in response to ctacke's comment on Jon Skeet's answer (It's not long for a comment)
I always thought foreach was pretty well known to be slower than a for loop since it has to use the iterator.
Actually, no, in this case foreach would be faster. Index access is bounds checked (ie. i is check to be in range three time in the loop: once in the for() and once each for the two ca[i]s), which makes a for loop slower than foreach.
If the C# compiler detects the specific syntax:
for(i = 0; i < ca.Length; i++)
then it will perform a ad hoc optimization, removing the internal bound-checks, making the for() loop faster. However, since here we must treat ca[0] as a special case (to prevent a leading space on the output), we can't trigger that optimization.