Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stringbuilder Flush 2

Status
Not open for further replies.

fluxdemon

MIS
Nov 5, 2002
89
US
Is there a member that allows a stringbuilder to be reset to ""?
 
There is a member that allows it, but what might be surprising at first is that it isn't a method, but a property.

Set "Length" to zero. Length is writeable in the StringBuilder class. The internal buffer will be untouched (which is good for performance), and subsequent "Appends" will overrwrite the old contents.

StringBuilder already must keep the allocated buffer length distinct from the in-use length. Making the property read/write just takes it to the next logical step. If you are repeatedly building up a StringBuilder to the same length and resetting it, you will find that after the first cycle there will be no addition heap allocations. Good performance is probably just what you are after (or you would have just allocated a new StringBuilder in your code in the first place instead of looking for a way to reset its contents).

(You might be curious what happens if you set the length to a value *higher* than the original length. In that case, the additional space is filled with nulls.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top