3 ms·
String declaration in iteration
Hi there,
This morning a was writing a piece of code and it made me wonder what the best possible way is to write this code.
Because a string is immutable in .NET, is it really that bad to create a new instance for each iteration?
Your thoughts on this please.
*For a short code illustration, check out http://silverappy.wordpress.com/2011/04/21/string-declaration-in-iteration/
- gus_massa 16y agoIf myArray is large (maybe >10), you should use a StringBuilder. But optimization is tricky and YMMV. Some article about this (for a few strings) http://www.codinghorror.com/blog/2009/01/the-sad-tragedy-of-micro-optimization-theater.html http://www.codinghorror.com/blog/2009/01/the-sad-tragedy-of-... And a simmilar question in SO: http://stackoverflow.com/questions/21078/whats-the-best-string-concatenation-method-using-c http://stackoverflow.com/questions/21078/whats-the-best-stri... I think that StackOverflow (http://stackoverflow.com http://stackoverflow.com) is a better site to get answers to this kind of questions
- silverappy 16y agoThanks gus!