It is a little difficult to answer. The above answer has a problem, StringBuilder has no virtual methods. Thus, you canβt do anything to break the class or make something "unnecessary" unsafe.
I think the probable reason is that the CLR has a special knowledge of this class. This is a bit commonplace for StringBuilder, compared to other .NET types with which it is intimate, pinvoke marker knows what the class looks like. You use it when you need to pass a string reference to unmanaged code, allowing it to write string content. Necessary, because it is not legal for String, it is immutable. The pinvoke router knows how to properly set the internal elements of a StringBuilder after calling pinvoke. But I donβt know how to do this for your derived class. This risk of slicing is not entirely worth noting to seal it. In particular, since it does not have virtual methods, therefore, you cannot completely override its behavior.
An extension method otherwise is a very reasonable solution.
source share