Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi
Can any one explain me "string immutable in c#"
Posted
Comments
Sergey Alexandrovich Kryukov 7-Jul-12 23:03pm    
The answer: "yes, many can explain that". :-)
Was that a question?
--SA

All .NET Strings are immutable - which means that once they are created, they cannot be changed in any way.
Every time you add or remove a character from a string, a new copy is created, and the previous version is unchanged.
An example:
C#
string orig = "Hello there!";
string copy = orig;
string replaced = orig.Replace("there", "World");
orig += " This is a test.";
Console.WriteLine(replaced);
Console.WriteLine(orig);
Console.WriteLine(copy);
The output is:
Hello World!
Hello there! This is a test.
Hello there!
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900