"const" or "static" string?

Hey guys, what’s better to use:

const string

OR

static string

I’m hoping to avoid unnecessary heap mismanagement and reduction of garbage collection.

Thanks!

They aren’t really the same thing. A const variable is one whose value cannot be changed. A static variable is one that is available from the class itself rather than any object (these are used like global variables for most purposes). A const value has no memory management overhead, since it never needs to be collected. A static variable per se has no advantages in memory management performance over an instance variable.

Can you give any more detail about what you need to do with strings? Performance often comes down more to the algorithms used and sensible allocation than anything else.

6 Likes