Solved

Immutable strings in C# I need help understanding

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
asp.net bundle 8 36
C# Update Status Bar.... 1 24
AutoHotkey ActiveX Component Error 8 56
install app on other machine 13 16
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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now