This is not a question of premature optimization per se. On the garbage collector and memory in general, what would hundreds of ToUpper() operations (many could be duplicated) do to a program, mainly in regard to the immutability of strings?
This is not a question of premature optimization per se. On the garbage collector
Share
Each call to
ToUpperwill create a new string instance, even if the contents is the same as the original, and even if the string already exists as an interned string literal.So, the impact of hundreds of
ToUppercalls is that you create hundreds of string instances. If the strings are short, this is not a problem, especially if you only use the strings for a short time. The garbage collector is made to handle small, short lived objects efficiently.Example: