Tech Off Post

Single Post Permalink

View Thread: Why are string types immutable in C#?
  • User profile image

    BTW, immutable types are inherently callee-safe (you can pass them to a method without fear they'll be changed) and thread-safe (no methods on the type change its state, so there's nothing to synchronize).

    Sven Groot wrote:
    Compare this to the C++ std::string object (which is not immutable), which is passed by value. This means that if you want to use a String as a key in a Hashtable, you're fine in C++, because C++ will copy the string to store the key in the hashtable (actually std::hash_map, but still) for later comparison.

    Is that the case or does std::string implement copy-on-write like MFC does? (I'm a little concerned that I no longer remember...)