Click here to Skip to main content
15,887,288 members
Please Sign up or sign in to vote.
1.12/5 (4 votes)
See more:
Hi Guys,
Explain about mutable strings and immutable in C#. When will i use the String builder rather than Strings, bcoz in Live project i handled every strings/Text in String Datatype.




Regards,
Saran.t
Posted

 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Feb-11 0:24am    
At least one reference is good, so 5.
As usual, I just formulated the answer out of my head, please see.
--SA
All .NET stings are immutable.
You cannot modify any string at all. All string operations create yet another string.
For example, if you have strings S1 and S2, S3 = S1 + S2 is a new string, while S1 and S2 remain the same.

In connection to that, imagine a loop adding to some string S (initially String.Empty) more and more portions at the end — a very typical task. As initial string S is never modified, actually, the the old value of S is concatenated with the new portion at the end and copied back to S. In this way, S is being assigned to a growing new string and all the data is copied every time.

In contrast System.Text.StringBuilder.Append does this thing much more effectively, because already available data is never copied again, the instance of StringBuilder mutates instead. If just two strings are added using "+" operations, it does not matter.
Same thing about comparison of string operations with StringBuilder operations.

As a rule of thumb, if you have any repeated operation to compose a string, always use StringBuilder. Basically, using more than one string "+" in one expression is not very effective; string.Format function is much better, faster and more readable.

—SA
 
Share this answer
 
v3
Comments
Albin Abel 12-Feb-11 0:41am    
my 5
[no name] 26-Dec-13 12:41pm    
Also my 5. Thank you for this lesson which is really appreciated. Regards, Bruno
Sergey Alexandrovich Kryukov 12-Feb-11 1:04am    
Thank you.
--SA
Sergey Alexandrovich Kryukov 26-Dec-13 13:12pm    
And thank you, Bruno.
—SA
Sandeep Mewara 12-Feb-11 0:48am    
Propose as answer! 5!
:)
A string instance is immutable. You cannot change it after it was created. Any operation that appears to change the string instead returns a new instance:

string foo = "Foo";
// returns a new string instance instead of changing the old one
string bar = foo.Replace('o', 'a');
string baz = foo + "bar"; // ditto here
 
Share this answer
 
Mutable and immutable are English words meaning "can change" and "cannot change" respectively. The meaning of the words is the same in the IT context; i.e.

a mutable string can be changed, and
an immutable string cannot be changed.

The meanings of these words are the same in C# / .NET as in other programming languages / environments, though (obviously) the names of the types may differ, as may other details.
 
Share this answer
 
Comments
Murali Maddu 3-Mar-16 5:03am    
In most case we do operations on Strings so it always to better to use StringBuilder rather than String then why didn't string was depricated by Microsoft. To continue the string object legacy i don't think so..any answer is much appreciated..

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