Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

String.IsInterned Method

Retrieves a reference to a specified String.

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

public static string IsInterned (
	string str
)
public static String IsInterned (
	String str
)
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.

// 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.
*/

// 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

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.