If this topic interests you, I have a number of articles on immutable programming at http://blogs.msdn.com/b/ericlippert/archive/tags/immutability/
I was just curious to know why structures, strings, etc. immutable?
Structures and classes are not immutable by default, although it is best to make structures immutable. I also like immutable classes.
Lines are immutable.
What is the reason for their immutability and the rest of the objects as mutable.
Reasons to make all types immutable:
It's easier to talk about objects that don't change. If I have a line with three elements in it, I know that it is not empty, it was not empty five minutes ago, it will not be empty in the future. It is immutable! As soon as I find out about this fact, I can use this fact forever. Facts about immutable objects do not become obsolete.
A special case of the first point: immutable objects are much easier to make thread safe. Most thread safety concerns relate to writing on one thread and reading on another; immutable objects have no records.
Immutable objects can be split and reused. For example, if you have an immutable binary tree, you can use its left and right subtrees as subtrees of another tree without worrying about it. In a mutable structure, you usually make copies of the data for reuse, because you do not want changes to one logical entity affect another. This can save a lot of time and memory.
Reasons for Creating Immutable Structures
There are many reasons to make structures unchanged. Here is just one.
Structures are copied by value, not by reference. It is easy to accidentally treat the structure as copied by reference. For example:
void M() { S s = whatever; ... lots of code ... s.Mutate(); ... lots more code ... Console.WriteLine(s.Foo); ... }
Now you want to refactor part of this code into a helper method:
void Helper(S s) { ... lots of code ... s.Mutate(); ... lots more code ... }
WRONG! It should be (ref S s) - if you do not, then the mutation will occur on copy s. If you do not allow mutations in the first place, all these problems disappear.
Reasons for immutable strings
Remember my first moment about facts about immutable structures that remain facts?
Suppose the line is changed:
public static File OpenFile(string filename) { if (!HasPermission(filename)) throw new SecurityException(); return InternalOpenFile(filename); }
What if the hostile caller changes the file name after a security check and before opening the file? The code simply opened a file to which they may not have permission!
Again, mutable data is hard to justify. You want "this caller to have the right to see that the file described by this line" is true forever until a mutation occurs. With the modified lines, in order to write protected code, we constantly had to make copies of the data, which, as we know, do not change.
What are the things that are believed to make an object immutable?
Is a type logically representing something that is an βeternalβ value? The number 12 is the number 12; he does not change. Integers must be immutable. Point (10, 30) is point (10, 30); he does not change. Points must be unchanged. The string "abc" is the string "abc"; he does not change. Lines must be immutable. The list (10, 20, 30) does not change. And so on.
Sometimes a type represents things that change. Last Name Mary Smith - Smith, but tomorrow she may be Mary Jones. Or Miss Smith today may be Dr. Smith tomorrow. The alien has fifty health points, but he has ten after being hit by a laser beam. Some things are best represented as mutations.
Is there a difference in the methods of allocating and freeing memory for mutable and immutable objects?
Not as such. As I mentioned earlier, one of the nice things about fixed values ββis that you can reuse portions of them without copying. Therefore, in this sense, the distribution of memory can be very different.