0

Let's assume that you have a string variable.

var t = "this is a test"

and we reassign another string to this variable

t = "this is another test"

what exactly happens with the data of the first assignment? how does the framework handle this in relation to GC?

Mantzas
  • 2,463
  • 3
  • 25
  • 35
  • the first allocated string will be collected (asuming that exact text isn't used as another string somewhere else) – Mitch Wheat Jan 20 '17 at 08:15
  • @HenkHolterman i am exactly asking about strings (interning), so the question is not too broad and probably not too beginner since you are unsure yourself. I have to modify the title... – Mantzas Jan 20 '17 at 08:50

1 Answers1

2

String Literals are stored in the String Intern Pool and follow a different managing procedure than any other object which is regularly collected by the CG.

It then really comes down on how the String Intern Pool will manage the "this is a test" literal.

Most likely is that it won't be cleared and stay in the literal pool. But for more reference on how the String literal pools works below .

https://msdn.microsoft.com/en-us/library/aa691090(v=vs.71).aspx

http://dailydotnettips.com/2012/02/12/the-string-intern-pool/

In C#, should I use string.Empty or String.Empty or "" to intitialize a string?

Community
  • 1
  • 1
Anestis Kivranoglou
  • 7,728
  • 5
  • 44
  • 47
  • 1
    The GC doesn't handle them separately, the strings are simply stored in a data structure in a rooted location. From the GC's perspective they're simply always active; just like any other object(s) you store in a variable. The GC doesn't have any sort of special knowledge of interned strings. – Servy Jan 20 '17 at 20:15