Solved

Immutable strings in C# I need help understanding

Posted on 2008-10-29
3
503 Views
Last Modified: 2013-12-17
Can someone explain to me the concept around immutable strings in C#. I thought immutable means that you can not change the content of a string. Yet in the compliler, the following code builds and runs and will ineed change the content of my string:

        String myString="hello";
        myString = myString + "GoodBye";
//  myString now = "helloGoodBye"
0
Comment
Question by:brgdotnet
3 Comments
 
LVL 3

Assisted Solution

by:Blackninja2007
Blackninja2007 earned 30 total points
ID: 22831391
While the string type is perfect when you wish to represent basic string variables (first name, SSN, etc.),
it can be inefficient if you are building a program that makes heavy use of textual data. The reason has
to do with a very important fact regarding .NET strings: the value of a string cannot be modified once
established. C# strings are immutable.

****
On the surface, this sounds like a flat-out lie, given that we are always assigning new values to
string variables. However, if you examine the methods of System.String, you notice that the methods
that seem to internally modify a string in fact return a modified copy of the original string.

*****

For example,
when you call ToUpper() on a string object, you are not modifying the underlying buffer of an existing string object, but receive a new string object in uppercase form:

0
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 30 total points
ID: 22831386
What it's really doing is creating 3 strings:
"hello"
'GoodBye"
"helloGoodBye"

Though your variables give access to only the one value at a time ... you've chewed up that much memory.

That's why the StringBuilder is recommended for such stuff.  Less memory chewed up, less time allocating, less garbage collection.
0
 
LVL 5

Expert Comment

by:johnaryan
ID: 22831394
The definition is correct. When you create a string in .Net; space on the Heap is allocated to that string.
Now when you alter the string, the old one is marked for garbage collection and space for the altered string is allocated on the heap.

It goes like this:
1. myString is loaded from memory to a register.
2. myString + "Goodbye"
4. the original heap allocation for myString is marked for GarbageCollection.
5. New memory is allocated for the altered myString
6. altered myString coppied to the new Heap location.

As you can see, technically strings are immutable internally.

Hope this helps,
John
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question