5

I have recently started to read up on mutable and immutable objects in C# and the constant thing i find wherever i read is hat being immutable makes things threadsafe and useful when used as keys in hashtables but what i dont understand is as far as the concept goes while we cannot change the content we can change the reference that is :

string s = "Hi";
s = "Bye";

While here the reference of s is changed to "Bye" but the main thing is that the content of s (or rather what it was pointing to) has changed and from the point of view of programming that is the same, so how does this make a particular function threadsafe or usable in hashtable if the string is changed ??

2 Answers2

2

Simple. If you were to pass s to code that runs on a different thread, this code will receive the string pointed to by s at the time the parameter is passed. Like all strings in .net, it will not change over time, so your threaded code does not need to take into account that you may reassign s to a different value.

If you assign "Bye" to s, the original string lives on (until its garbage collected), and your variable s points to a new string.

In dictionaries, it is slightly different. If you change your mutable key in a way such that its hashcode changes, the dictionary will fail to find the key: the hashcode is used to search in an index, and the dictionary will not find the correct record if the hashcode changes over time. So this does not so really require immutability, but immutability will ensure consistent computation of hashcodes.

What immutability does for you is it gives the ability to think of the object as if it were a value type (such as int), which is often easier to reason about.

Daniel Abou Chleih
  • 2,440
  • 2
  • 19
  • 31
  • ok......but tell me this assuming that string was mutable...if i passed it as a variable to a code that runs on a different thread..it would have the version i passed and would work with that data even if i changed it later...right ?? – and your mother Sep 29 '13 at 20:21
  • also in the dictionary e.g. you are saying that if the hash would change, the key would not be found but wouldn't the same happen with immutable string ?? i changed the data it refers to so even if the string is in memory for GC it points to a variable which does not contain that data... – and your mother Sep 29 '13 at 20:30
  • As for your first comment: if strings were mutable, it would be possible to change a string after you passed it to a different thread, and that thread could see the change. –  Sep 30 '13 at 08:00
0

In your example, s is reassigned to reference a different string object ("Bye"), but the object that s previously referenced ("Hi") hasn't changed. Anything else that has a reference to the string "Hi" (another thread, a Dictionary, etc.) will be unaffected. As you mention, string is immutable - it's content cannot be changed once created. If you append one string to another, for example, you get a new string object. The two original string objects remain the same. This is what makes string thread-safe, and suitable for use in a hashtable.

The reference s isn't thread-safe - to ensure thread safety when using a reference, you'd need to put a lock around the reference assignment to ensure that one thread didn't attempt to read from the reference while another thread was writing to it.

Chris Mantle
  • 6,595
  • 3
  • 34
  • 48
  • "Anything else that has a reference to the string "Hi" (another thread, a Dictionary, etc.) will be unaffected" - will the original string not go for GC in that case ?? and if it goes for GC, do the thread or dict break down....i mean what happens ?? what does it refer to in that case ?? also my second comment to jdv-Jan de Vaan above about dictionaries applies here too...what would happen in that case ?? – and your mother Sep 29 '13 at 20:35