Solved

Immutable strings in C# I need help understanding

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Object Oriented Programming, C#, referencing, scoping. 13 69
C# LINQ ForEach() question 6 54
MailAddress in vb 4 29
Dynamics CRM Plugin Query TotalRecordCount returning -1 13 43
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 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