very useful link for State Mangement on MSDN
http://msdn.microsoft.com/en-us/library/75x4ha6s(v=VS.100).aspx
http://msdn.microsoft.com/en-us/library/75x4ha6s(v=VS.100).aspx
String.Intern (string)
method searches the intern pool for a string equal to the specified value. If such a string exists, its reference in the intern pool is returned. Otherwise, a reference to the specified string is added to the intern pool and that reference is returned. declared
with a value of "Intern pool" is interned; because, it is a string literal. The string built
is a new string object with the same value as declared
but generated by the System.Text.StringBuilder
class. The Intern
method searches for a string with the same value as built
. Since the string already exists in the intern pool, the method returns the same reference that is assigned to declared
and assigns that reference to interned
. declared
and built
compare unequal because they refer to different objects, while references declared
and interned
compare equal because they refer to the same string.String declared = "Intern pool"; String built = new StringBuilder().Append("Intern ") .Append("pool").ToString(); String interned = String.Intern(built); Console.WriteLine ((Object)built==(Object)declared); // different references Console.WriteLine ((Object)interned==(Object)declared); // same reference
String
objects is unlikely to be released until the CLR terminates: the CLR's references to interned String
objects may persist after your application or application domain terminates. Secondly, to intern a string, a string must first be created. Thus, despite the fact that the memory will eventually be garbage collected, the memory used by the String
object will still be allocated.