Retrieves a reference to a specified string.
A reference to str if it is in the common language runtime intern pool; otherwise, null.
Type Reason ArgumentNullException str is a null reference.
The common language runtime automatically maintains a table, called the intern pool, which contains a single instance of each unique literal string constant declared in a program, as well as any unique instance of string you add programmatically by calling the string.Intern(string) method.
The intern pool conserves string storage. If you assign a literal string constant to several variables, each variable is set to reference the same constant in the intern pool instead of referencing several different instances of string that have identical values.
This method looks up str in the intern pool. If str has already been interned, a reference to that instance is returned; otherwise, null is returned.
Compare this method to the string.Intern(string) method.
This method does not return a Boolean value. If you call the method because you want a Boolean value that indicates whether a particular string is interned, you can use code such as the following.
code reference: System.String.IsInterned#1
Starting with the .NET Framework version 2.0, you can override the use of the intern pool when you use the Native Image Generator (Ngen.exe) to install an assembly to the native image cache on a local computer. For more information, see Performance Considerations in the Remarks section for the string.Intern(string) property.
The following example demonstrates the string.IsInterned(string) method.
C# Example
using System; using System.Text; public class StringExample { public static void Main() { String s1 = new StringBuilder().Append("My").Append("Test").ToString(); Console.WriteLine(String.IsInterned(s1) != null); } }
The output is
True