Use StringBuilder on string Refactor! Pro feature

by Miha Markič 22. February 2007 15:23

One of the wellcome refactoring features of [DX] Refactor! Pro is "replace string with StringBuilder". Why would you want to do that? String building into a string variable is slow because each time you change the value of string variable the memory for new value is allocated, new value is stored into this new memory place and old memory block is released for garbage collection. So, if you have a bunch of string modifications, such as building a text, you should avoid using string variable and instead use StringBuilder class that deals with memory much more gently (note that if you have few modifications string variable is still a better option). That's why Use StringBuilder refactoring comes handy. Imagine this piece of immaginary code that happens to exist here and there:

int[] list = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; string concatenated = string.Empty; foreach (int item in list) { if (concatenated.Length > 0) concatenated += ", "; concatenated += item.ToString(); } return c

If the number of items would be greater this code will become relatively sluggish to execute. Obviously there is a need of StringBuilder but instead of manually rewritting the code you can do this: select the code and press Refactor! key, pick Use StringBuilder: concatenated from Refactor! menu:

Refactor! will preview what changes it will do to replace the string with StringBuilder. Confirm the change and here is what you'll get:

int[] list = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; StringBuilder concatenatedBuilder = new StringBuilder(); foreach (int item in list) { if (concatenatedBuilder.Length > 0) concatenatedBuilder.Append(", "); concatenatedBuilder.Append(item.ToString()); } return concatenatedBuilder.ToString();

Needless to say that no coding was required and the code will perform much better, specially with bigger lists. Cool, eh.

Tags:

.net

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Miha Markic

About me
Righthand
 
Microsoft MVP
 
Developer Express' DXSquad
INETA Country Leader for Slovenia
INETA Country Leader for Slovenia

Slovene Developer Users Group Lead
Friends of Red-Gate
LLBLGenPro Partner

Miha currently works as a free lance consultant and software developer specialized in .net area.
He graduated in Computer and information science at the University of Ljubljana, Slovenia. He has accumulated experience in various programming languages such as Java, Visual Basic 3-6 (MCP), Visual C++, Delphi, C# and VB.Net through years.
He has experience in practically all (technical) stages of project development, including planning, framework development, user interface, business processes, as well as testing and documenting. He has worked on big and small projects in Slovenia and abroad (e.g. participated in completing level 3 IS for the Nucor steel plant, Hertford, USA).
Currently he enjoys programming in .net environment using C#. Since 2000 he has been active in Developer Express' DX Squad and has been ECDL trainer and tester. He also gives lectures on conferences and other events in Slovenia.

Month List

Tag cloud

Most comments

Paulius Paulius
1 comments
us United States
Meh Meh
1 comments
us United States
bart dm bart dm
1 comments
nl Netherlands

RecentComments

Comment RSS