interviews.dotnetthread.com

↑ Grab this Headline Animator

Friday, April 9, 2010

What do you mean by String objects are immutable?

String objects are immutable as its state cannot be modified once created.
Every time when we perform any operation like copy, replace, case conversion or when we pass a string object as a parameter to a method a new object will be created.

Example:

String s = "ABC";
s.Replace("A","X");

Here Replace() method will not change data that s contains, instead a new string object is created to hold data "XBC" and the reference to this object is returned by Replace() method.

So in order to point s to this object we need to write below line.
s = s.Replace("A","X");

Submit this story to DotNetKicks

No comments:

Post a Comment

Post your comments/questions/feedback for this Article.

 

Latest Articles