What is StringInterning?

5 views
Skip to first unread message

.NetIndia

unread,
Nov 2, 2005, 1:46:00 AM11/2/05
to dotNetIndia
 
What is StringInterning?

--
Regards,
.NetIndia Group,
"There is no delight in owning anything unshared."
"A book lying idle on a shelf is wasted ammunition. Like money, books must be kept in constant circulation. Lend and borrow to the maximum -- of both books and money! But especially books, for books represent infinitely more than money. A book is not only a friend, it makes friends for you. When you have possessed a book with mind and spirit, you are enriched. But when you pass it on you are enriched threefold."

Blog:http://dotnetindia.blogspot.com/
Technical Discussion: Techdot...@googlegroups.com
http://groups.google.com/group/TechdotNetIndia
Job Section         : TechdotNet...@googlegroups.com
http://groups.google.com/group/TechdotNetIndia-Jobs

Shivprasad Koirala

unread,
Nov 2, 2005, 2:08:03 AM11/2/05
to .NetIndia
Its the property of a SAX driver to get XML string with out method
calls
-------
Regards ,
C#, VB.NET , SQL SERVER , UML , DESIGN Patterns Interview question book
http://www.geocities.com/dotnetinterviews/
My Interview Blog
http://spaces.msn.com/members/dotnetinterviews/

BabuLives

unread,
Nov 3, 2005, 7:04:19 AM11/3/05
to .NetIndia
Hai groups...

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

Reply all
Reply to author
Forward
0 new messages