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 🙂

Theme music for this blog post


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s