So I just finished Writing High-Performance .NET Code, which is dry at times, but contains nuggets of wisdom.
One of them was something I should’ve understood, but really only now dawned on me.
Strings are immutable.
But like, really immutable.
This means any operation on them like calling ToUpper, results in the creation of a new string.
This behavior makes sense ofcourse, since they are immutable. You’re even reminded in the docs:
This method does not modify the value of the current instance. Instead, it returns a new string in which all characters in the current instance are converted to uppercase.
But intuitively while programming in your IDE, I will easily forget that I’m creating a new string. Unwittingly you could be increasing pressure on the garbage collector, deteriorating application performance, if you don’t pay attention in a hot application loop.
And I understand the string abstraction shouldn’t leak implementation details. But on the other hand, a name that is honest about what you are doing like .CopyToUpper, would save a lot of wasted CPU cycles in applications passed and to come 🙂