Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Search Your Question

Thursday, April 24, 2008

What do you understand by the term "immutable"?

Immutable means you can't change the currrent data,but if you perform some operation on that data, a new copy is created. The operation doesn't change the data itself.

For example let's say you have a string object having "hello" value. Now if you say
temp = temp + "new value" new object is created, and values is saved in that. The temp object is immutable, can't be changed. An object qualifies as being called immutable if its value cannot be modified once it has been created.

For example, methods that appear to modify a String actually return a new String containing the modification. Developers are modifying strings all the time in their code. This may appear to the developer as mutable - but it is not.

What actually happens is your string variable/object has been changed to reference a new string value containing the results of your new string value. For this very reason .NET has the System.Text.StringBuilder class.

If you find it necessary to modify the actual contents of a string-like object heavily, such as in a for or foreach loop, use the System.Text.StringBuilder class.

No comments:

Archives