?
Solved

Immutable strings in C# I need help understanding

Posted on 2008-10-29
3
Medium Priority
?
508 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Assisted Solution

by:Blackninja2007
Blackninja2007 earned 120 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 120 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

752 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