perform poorly

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

Hi guys,
why does this code performs poorly?

static string Space (string s)

{

string s2 = "";

foreach (char c in s)

{

s2 += c;

s2 += " ";

}

return s2;

}
 
Becaúse the string concatenation does instead of simply appending the char
to the string, it does create a completely new string each time, this is
because the string class is immutable. to avoid this you should uses the
class StringBuilder instead.
 
Strings are immutable.

Use a System.Text.StringBuilder type instead, it's much more efficient for
large string concatenation operations.

HTH

Glenn
 
This time you managed to get someone to answer the essay question for you.
What is that... five threads where you are asking questions from your
take-home test? Are you going to post the whole test?

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 
Yes, but are you helping your friend do their essay? Helping someone cheat
is just as bad as being a cheat yourself...
 
Back
Top