
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
74
|
Chapter 2: Strings and Characters
is returned, there is no match in the intern pool, and thus there is no match between
the string variable’s value and any string literals:
string s1 = "f";
string s2 = "oo";
string s3 = s1 + s2;
if (String.IsInterned(s3) != null)
{
// If the string "foo" has been defined in the app and placed
// into the intern pool, this block of code executes.
}
else
{
// If the string "foo" has NOT been defined in the app NOR been placed
// into the intern pool, this block of code executes.
}
Exercise caution when using the string interning methods. Calling the Intern method
for every string that your application creates will actually cause the application’s per-
formance to slow considerably, since this method must search the intern pool for the
string.
Another potential problem with the
IsInterned method in particular stems from the
fact that every string literal in the application is stored in this intern pool at the start
of the application. If you are using
IsInterned to determine whether a string exists,
you are comparing that string against all string literals that exist in the application, as
well as any you might have explicitly interned, not just the ones in the scope in
which
IsInterned is used.
See Also
See the “String.Intern Method” and “String.IsInterned