I don't know much about ASP.NET (yet), but I do know about polymorphism...
The code in listing 10-7 (and subsequent listings where the same technique is used), are not using the atomized strings...
From listing 10-7:
NameTable nt = new NameTable();
object book = nt.Add( "book" );
if ( ... && book.Equals( reader.LocalName ) )
method returns a string
. So, the book
variable, even though it's of type object
, references a string object.
overrides the Equals()
method, the comparison will actually be a string
comparison, and not an object reference comparison, as was intended. Polymorphism will cause the string
override of Equals()
to be called.
What the code should
be in order to optimize it with the use of atomized strings is:
if ( ... && ReferenceEquals( book, reader.LocalName ) )
is implemented by System.Object
and compares the object references. book
is a reference to the atomized string, so it will actually be compared to the atomized Reader.LocalName