The simplest and most effective way is to execute this command:
data.Length--;
By doing this, you move the pointer (i.e. the last index) back one character, but you do not change the variability of the object. In fact, clearing a StringBuilder best done using Length (but actually use the Clear() method for clarity instead, because it looks like its implementation):
data.Length = 0;
again, since it does not change the distribution table. Think about it, how to say, I no longer want to recognize these bytes. Now, even when calling ToString() it does not recognize anything for its Length , well, it cannot. This is a mutable object that allocates more space than the one you provide it, it is simply built in this way.
Mike Perrenoud Jun 20 '13 at 13:40 2013-06-20 13:40
source share