This documentation is archived and is not being maintained.

String.IsInterned Method

Updated: September 2011

Retrieves a reference to a specified String.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static string IsInterned(
	string str


Type: System.String

A String.

Return Value

Type: System.String
A String reference to str if it is in the common language runtime "intern pool"; otherwise null.


str is null.

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.

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 Intern 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.

using System;

public class Example
   public static void Main()
      string str1 = "a";
      string str2 = str1 + "b";
      string str3 = str2 + "c";
      string[] strings = { "value", "part1" + "_" + "part2", str3, 
                           String.Empty, null };
      foreach (var value in strings) {
         if (value == null) continue;

         bool interned = String.IsInterned(value) != null;
         if (interned)
            Console.WriteLine("'{0}' is in the string intern pool.", 
            Console.WriteLine("'{0}' is not in the string intern pool.",
// The example displays the following output: 
//       'value' is in the string intern pool. 
//       'part1_part2' is in the string intern pool. 
//       'abc' is not in the string intern pool. 
//       '' is in the string intern pool.

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 Intern property.

The following example demonstrates that literal strings are interned automatically by the compiler.

// Sample for String.IsInterned(String) 
using System;
using System.Text;
using System.Runtime.CompilerServices;

// In the .NET Framework 2.0 the following attribute declaration allows you to  
// avoid the use of the interning when you use NGEN.exe to compile an assembly  
// to the native image cache.
[assembly: CompilationRelaxations(CompilationRelaxations.NoStringInterning)]
class Sample
    public static void Main()
        // String str1 is known at compile time, and is automatically interned.
        String str1 = "abcd";

        // Constructed string, str2, is not explicitly or automatically interned.
        String str2 = new StringBuilder().Append("wx").Append("yz").ToString();
        Test(1, str1);
        Test(2, str2);

    public static void Test(int sequence, String str)
        Console.Write("{0}) The string, '", sequence);
        String strInterned = String.IsInterned(str);
        if (strInterned == null)
            Console.WriteLine("{0}', is not interned.", str);
            Console.WriteLine("{0}', is interned.", strInterned);

//This example produces the following results: 

//1) The string, 'abcd', is interned. 
//2) The string, 'wxyz', is not interned. 

//If you use NGEN.exe to compile the assembly to the native image cache, this 
//example produces the following results: 

//1) The string, 'abcd', is not interned. 
//2) The string, 'wxyz', is not interned.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0




September 2011

Noted that the method does not return a Boolean value.

Customer feedback.