InterviewSolution
| 1. |
What Do You Mean By String Objects Are Immutable? |
|
Answer» String objects are immutable MEANS, they cannot be CHANGED after they have been created. All of the String methods and C# operators that appear to modify a string actually return the results in a new string object. In the following example, when the contents of s1 and s2 are CONCATENATED to form a single string, the two original strings are unmodified. The += operator creates a new string that CONTAINS the combined contents. That new object is assigned to the variable s1, and the original object that was assigned to s1 is released for garbage collection because no other variable holds a REFERENCE to it. string s1 = "First String "; // Concatenate s1 and s2. This actually creates a new System.Console.WriteLine(s1); String objects are immutable means, they cannot be changed after they have been created. All of the String methods and C# operators that appear to modify a string actually return the results in a new string object. In the following example, when the contents of s1 and s2 are concatenated to form a single string, the two original strings are unmodified. The += operator creates a new string that contains the combined contents. That new object is assigned to the variable s1, and the original object that was assigned to s1 is released for garbage collection because no other variable holds a reference to it. string s1 = "First String "; // Concatenate s1 and s2. This actually creates a new System.Console.WriteLine(s1); |
|