Disposing a StringBuilder object

No, a StringBuilder is a purely managed resource. You should just get rid of all references to it. Everything else is taken care of by the garbage collector:

StringBuilder sb = ...;
// ... do work
sb = null; // or simply let it go out of scope.

In .NET, there’s no deterministic delete (like C++, where you free up memory allocated to a single object.) Only GC can free memory. By forfeiting all references to an object, you’ll let GC be able to deallocate the object if it wants to. You can force a garbage collection by calling the System.GC.Collect method. However, it’s not recommended to manipulate with GC unless you really know what you are doing. GC is smart. It’s rarely beneficial to force it.

Leave a Comment

tech