Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
As strings in c# are immutable and lets suppose we have below code

C#
string str=string.Empty;               //First instance
for(int i=0;i<5;i++)
{
     str = str+i.ToString();           // Next five instances
}


There will be 6 instances created after this code executes. So below are some confusions.
1) finally str will be pointing to 6th instance, right?
2) who will be pointing to previous 5 instances?
3) Can we access previous 5 instances any how?

Also please mention some good links on this, if you have any.
Posted
Updated 27-May-13 7:21am
v2

1) yes
2) no one
3) you can't they will be garbage collected unless you keep the in a list yourself.
 
Share this answer
 
Comments
Ian A Davidson 27-May-13 13:42pm    
Spot on +5.
However, and I'm not sure about this, but my guess is that the reference returned by string.Empty will always exist, and you can access it by another call to string.Empty. :)
Mehdi Gholam 28-May-13 9:07am    
Thanks Ian!
Francisco T. Chavez 27-May-13 19:59pm    
3) mostly, there are a few ways to access those string if they haven't been garbage collected, but it would be a lot simpler to just store them yourself and access them. Also, .net tries not to have multiple instances of identical strings, so if you recreate one of those strings before it is garbage collected, .net might just end up giving you the original string instead (might).
Mehdi Gholam 28-May-13 9:07am    
Throwing the baby with the bath water... :)
Varun Sareen 28-May-13 4:08am    
5+
Mehdi Gholam is right, as far as it goes.

But please, do not use "pointing to" in normal C# - pointers are very different from references and it's not a good area to become confused in!

1) Yes, after the loop, str will be a reference to the final string created inside the loop: "01234"
2) No variable in your program will be referencing the "intermediate" string values, except the first one which is a special single instance string that is "shared" across your whole application. So if any other string refers to String.Empty (or "", they are the same thing) there will be a reference to it.
3) No...sort of. There are in fact ways to access the value of the variables on the heap before they are garbage collected (http://msdn.microsoft.com/en-GB/library/bb383561(v=vs.80).aspx[^] explains one of them) and even after garbage collection it is possible to access the values if you know how Windows memory management works. For example, a "Brute Force" method is to cause a different program to allocate enough memory to get your entire application data area paged out to disk and then examine the disk content. This is one of the reasons that the SecureString class exists: it's content is encrypted at all times to prevent memory examination from being effective.
 
Share this answer
 
Hello there,
If you want to have 6 different strings you may use an array, and add new value in each loop count, or you can make a long string with a separator "|" between every sentence.

I hope i understood your question right,
Good Luck,
z3ngew
 
Share this answer
 
If you want to access all the instances, then better to store in a string array like below.
C#
String[] str = new String[5];

for(int i=0; i < 5; i++)
{
     str[i] = i.ToString();
}

Now, str will contain the values in an array.
 
Share this answer
 
Comments
Varun Sareen 28-May-13 4:09am    
Good one dear but not the answer which the user is looking for.
Ok. I just give him a way how to access all instances as he wanted...

Anyway, thanks.
Varun Sareen 28-May-13 6:13am    
yea i got that dear that's why i said Good :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900