Solved

Immutable strings in C# I need help understanding

Posted on 2008-10-29
3
506 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 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

695 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