Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
System
String Class
String Methods
 IsInterned Method

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Class Library
String.IsInterned Method

Retrieves a reference to a specified String.

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

Visual Basic (Declaration)
Public Shared Function IsInterned ( _
    str As String _
) As String
Visual Basic (Usage)
Dim str As String
Dim returnValue As String

returnValue = String.IsInterned(str)
C#
public static string IsInterned (
    string str
)
C++
public:
static String^ IsInterned (
    String^ str
)
J#
public static String IsInterned (
    String str
)
JScript
public static function IsInterned (
    str : String
) : String

Parameters

str

A String.

Return Value

A String reference to str if it is in the common language runtime "intern pool"; otherwise a null reference (Nothing in Visual Basic).
Exception typeCondition

ArgumentNullException

str is a null reference (Nothing in Visual Basic).

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, a null reference (Nothing in Visual Basic) is returned.

Compare this method to the Intern method.

This method does not return a Boolean value, but can still be used where a Boolean is needed.

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

Visual Basic
' Sample for String.IsInterned(String)
Imports System
Imports System.Text

Class Sample
   Public Shared Sub Main()
      ' String str1 is known at compile time, and is automatically interned.
      Dim str1 As [String] = "abcd"
      
      ' Constructed string, str2, is not explicitly or automatically interned.
      Dim str2 As [String] = New StringBuilder().Append("wx").Append("yz").ToString()
      Console.WriteLine()
      Test(1, str1)
      Test(2, str2)
   End Sub 'Main
   
   Public Shared Sub Test(sequence As Integer, str As [String])
      Console.Write("{0}) The string, '", sequence)
      Dim strInterned As [String] = [String].IsInterned(str)
      If strInterned Is Nothing Then
         Console.WriteLine("{0}', is not interned.", str)
      Else
         Console.WriteLine("{0}', is interned.", strInterned)
      End If
   End Sub 'Test
End Class 'Sample '
'This example produces the following results:
'
'1) The string, 'abcd', is interned.
'2) The string, 'wxyz', is not interned.
'
C#
// Sample for String.IsInterned(String)
using System;
using System.Text;

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();
    Console.WriteLine();
    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);
    else
        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.
*/
C++
// Sample for String::IsInterned(String)
using namespace System;
using namespace System::Text;
void Test( int sequence, String^ str )
{
   Console::Write( "{0} The string '", sequence );
   String^ strInterned = String::IsInterned( str );
   if ( strInterned == nullptr )
      Console::WriteLine( "{0}' is not interned.", str );
   else
      Console::WriteLine( "{0}' is interned.", strInterned );
}

int 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 = (gcnew StringBuilder)->Append( "wx" )->Append( "yz" )->ToString();
   Console::WriteLine();
   Test( 1, str1 );
   Test( 2, str2 );
}

/*
This example produces the following results:

1) The string 'abcd' is interned.
2) The string 'wxyz' is not interned.
*/
J#
// Sample for String.IsInterned(String)
import System.*;
import System.Text.*;

class Sample
{
    public static void main(String[] args)
    {
        // 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();
        Console.WriteLine();
        Test(1, str1);
        Test(2, str2);
    } //main

    public static void Test(int sequence, String str)
    {
        Console.Write("{0}) The string, '", System.Convert.ToString(sequence));
        String strInterned = String.IsInterned(str);
        if (strInterned == null) {
            Console.WriteLine("{0}', is not interned.", str);
        }
        else {
            Console.WriteLine("{0}', is interned.", strInterned);
        }
    } //Test
} //Sample 
 /*
This example produces the following results:

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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker