Usage of EnsureCapacity for a string data type

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all,

I understand the definition of this property, but I'm not sure when or why
it should be used.
Thanks in advnace

Roy
 
Roy said:
I understand the definition of this property, but I'm not sure when or why
it should be used.

Note that it's a method of StringBuilder, not String.

You would use this if you had a StringBuilder which already had a
certain capacity (i.e. you couldn't just create it with the right
capacity) but you knew that in the process of the next set of
operations, it would need to grow to at least a certain size. Using
EnsureCapacity makes the capacity grow in one go, rather than perhaps
involving several "jumps", each of which would need to copy data.

Basically, it's an optimisation thing - I don't think I've ever used
it, personally.
 
Back
Top