System.String.Intern Method

Retrieves the system's reference to the specified string.

Syntax

public static string Intern (string str)

Parameters

str
A string to search for in the intern pool.

Returns

The system's reference to str, if it is interned; otherwise, a new reference to a string with the value of str.

Exceptions

TypeReason
ArgumentNullException str is a null reference.

Remarks

The common language runtime conserves string storage by maintaining a table, called the intern pool, that contains a single reference to each unique literal string declared or created programmatically in your program. Consequently, an instance of a literal string with a particular value only exists once in the system.

For example, if you assign the same literal string to several variables, the runtime retrieves the same reference to the literal string from the intern pool and assigns it to each variable.

The string.Intern(string) method uses the intern pool to search for a string equal to the value of str. If such a string exists, its reference in the intern pool is returned. If the string does not exist, a reference to str is added to the intern pool, then that reference is returned.

In the following example, the string s1, which has a value of "MyTest", is already interned because it is a literal in the program. The System.Text.StringBuilder class generates a new string object that has the same value as s1. A reference to that string is assigned to s2. The string.Intern(string) method searches for a string that has the same value as s2. Because such a string exists, the method returns the same reference that is assigned to s1. That reference is then assigned to s3. References s1 and s2 compare unequal because they refer to different objects; references s1 and s3 compare equal because they refer to the same string.

code reference: System.String.Intern#1

Compare this method to the string.IsInterned(string) method.

Version Considerations

In the net_v35SP1_long, the string.Intern(string) method reverts to its behavior in the .NET Framework 1.0 and 1.1 with regard to interning the empty string. In the following example, the variable str1 is assigned a reference to string.Empty, and the variable str2 is assigned the reference to string.Empty that is returned by calling the string.Intern(string) method after converting a System.Text.StringBuilder object whose value is string.Empty to a string. Then the references contained in str1 and str2 are compared for equality.

code reference: System.String.Intern#2

In the net_v10_short, net_v11_short, and net_v35SP1_short, str1 and str2 are equal. In the net_v20SP1_long and net_v30_long, str1 and str2 are not equal.

Performance Considerations

If you are trying to reduce the total amount of memory your application allocates, keep in mind that interning a string has two unwanted side effects. First, the memory allocated for interned string objects is not likely be released until the common language runtime (CLR) terminates. The reason is that the CLR's reference to the interned string object can persist after your application, or even your application domain, terminates. Second, to intern a string, you must first create the string. The memory used by the string object must still be allocated, even though the memory will eventually be garbage collected.

The .NET Framework version 2.0 introduces the System.Runtime.CompilerServices.CompilationRelaxations.NoStringInterning enumeration member. The System.Runtime.CompilerServices.CompilationRelaxations.NoStringInterning member marks an assembly as not requiring string-literal interning. You can apply System.Runtime.CompilerServices.CompilationRelaxations.NoStringInterning to an assembly using the System.Runtime.CompilerServices.CompilationRelaxationsAttribute attribute. Also, when you use the Native Image Generator (Ngen.exe) to compile an assembly in advance of run time, strings are not interned across modules.

Example

The following example demonstrates the string.Intern(string) method.

C# Example

using System;
using System.Text;
public class StringExample {
 public static void Main() {

     String s1 = "MyTest"; 
        String s2 = new StringBuilder().Append("My").Append("Test").ToString(); 
        String s3 = String.Intern(s2);

        Console.WriteLine(Object.ReferenceEquals(s1, s2));    //different
        Console.WriteLine(Object.ReferenceEquals(s1, s3));    //the same
    }
}

The output is

False
True

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0