CLR maintain a table called Intern Pool for string storage.Intern pool
contain single instant of each distinct literal string constant.
For example if you have
string strEx1="DotNet";
string strEx2="DotNet";
The .NET compiler search for "DotNet" in intern pool,if it doesn't find
an entry ,it will create a string in heap and reference to the heap is
value of the string.For second string ,compiler check for the entry,it
finds one,so it will not create an entry this time,instead it returns
the value.
So MessageBox.Show(Object.ReferenceEquals (strEx1,strEx2).ToString());
returns True
Now consider following code:
string strEx1="DotNet";
string strEx2="Dot"+"Net";
MessageBox.Show(Object.ReferenceEquals (strEx1,strEx2).ToString());
returns False
When you create string dynamically ,string will not be added into
intern pool
Now string strEx3= string.Intern(strEx2) Intern method ,search intern
pool for the value of string strEx2 (ie,"DotNet") .it finds an entry,so
the method returns the same reference that is assigned to strEx1.
so MessageBox.Show(Object.ReferenceEquals (strEx1,strEx3).ToString());
returns True
There is another methods called IsInterned to checks whether the value
in interned or not.if it is not interned it returns a null
reference,else not null
Regards,
Satheesh babu