For the StringBuilder class, why not overload the += operator instead of a using a unique .Append() method?
Append() only concatenates strings so why did they not just overload += operator like so:
StringBuilder sb = new StringBuilder();
sb += "My string";
Is it a case of efficiency? Is it a case of convention or intuitiveness?
Thanks,
Arithmetical operations should be limited to types that act like arithmetical values. It is bad enough that the sum of two strings is a third string. Conflating the addition operation with string concatenation is a questionable choice because string concatenation obeys very few of the rules of addition; in particular, a + b != b + a
.
But to go there for string builders -- which are by definition mutable state and not arithmetical values -- is horrid. The sum of two things should be a third thing that differs from the two summands. That is, a += b
must have the same semantics as a = a + b
and not a.MutateWith(b)
. If there isn't an assignment to a
at the end then compound assignment is the wrong operator.
More generally: never make cutesy operator overloads. Operator overloads are there so that you can make two complex numbers add to a third, not so that you can make an customer plus a jar of peanut butter equal a purchase order, or some such silliness.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With