String.IsInterned Method
Retrieves a reference to a specified String.
[Visual Basic] Public Shared Function IsInterned( _ ByVal str As String _ ) As String [C#] public static string IsInterned( string str ); [C++] 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).
Exceptions
| Exception Type | Condition |
|---|---|
| ArgumentNullException | str is a null reference (Nothing in Visual Basic). |
Remarks
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.
Example
[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 <mscorlib.dll> using namespace System; using namespace System::Text; void Test(int sequence, String* str) { Console::Write(S"{0} The string '", __box(sequence)); String* strInterned = String::IsInterned(str); if (strInterned == 0) Console::WriteLine(S"{0}' is not interned.", str); else Console::WriteLine(S"{0}' is interned.", strInterned); } int main() { // String str1 is known at compile time, and is automatically interned. String* str1 = S"abcd"; // Constructed string, str2, is not explicitly or automatically interned. String* str2 = (new StringBuilder())->Append(S"wx")->Append(S"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. */
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard