String.IsInterned Method
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Retrieves a reference to a specified String.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
<SecuritySafeCriticalAttribute> _
Public Shared Function IsInterned ( _
str As String _
) As String
[SecuritySafeCriticalAttribute]
public static string IsInterned(
string str
)
Parameters
- str
Type: System.String
The string to search for in the intern pool.
Return Value
Type: System.String
A reference to str if it is in the common language runtime intern pool; otherwise, nulla null reference (Nothing in Visual Basic).
Exceptions
Exception | Condition |
---|---|
ArgumentNullException | str is nulla 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, nulla null reference (Nothing in Visual Basic) is returned.
Important Note: |
---|
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. |
Module Example
Public Sub Demo(outputBlock As System.Windows.Controls.TextBlock)
Dim str1 As String = "a"
Dim str2 As String = str1 + "b"
Dim str3 As String = str2 + "c"
Dim strings() As String = { "value", "part1" + "_" + "part2", str3,
String.Empty, Nothing }
For Each value In strings
If value Is Nothing Then Continue For
Dim interned As Boolean = (String.IsInterned(value) IsNot Nothing)
If interned Then
outputBlock.Text += String.Format("'{0}' is in the string intern pool.",
value) + vbCrLf
Else
outputBlock.Text += String.Format("'{0}' is not in the string intern pool.",
value) + vbCrLf
End If
Next
End Sub
End Module
' 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.
using System;
public class Example
{
public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
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)
outputBlock.Text += String.Format("'{0}' is in the string intern pool.\n",
value);
else
outputBlock.Text += String.Format("'{0}' is not in the string intern pool.\n",
value);
}
}
}
// 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.
Compare this method to the Intern method.
Examples
The following code example demonstrates that literal strings are interned automatically by the compiler.
' Sample for String.IsInterned(String)
Imports System.Text
Imports 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 Example
Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
' 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()
outputBlock.Text &= vbCrLf
Test(outputBlock, 1, str1)
Test(outputBlock, 2, str2)
End Sub 'Main
Public Shared Sub Test(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal sequence As Integer, ByVal str As [String])
outputBlock.Text += String.Format("{0}) The string, '", sequence)
Dim strInterned As [String] = [String].IsInterned(str)
If strInterned Is Nothing Then
outputBlock.Text += String.Format("{0}', is not interned.", str) & vbCrLf
Else
outputBlock.Text += String.Format("{0}', is interned.", strInterned) & vbCrLf
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.
'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.
// 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 Example
{
public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
// 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();
outputBlock.Text += "\n";
Test(outputBlock, 1, str1);
Test(outputBlock, 2, str2);
}
public static void Test(System.Windows.Controls.TextBlock outputBlock, int sequence, String str)
{
outputBlock.Text += String.Format("{0}) The string, '", sequence);
String strInterned = String.IsInterned(str);
if (strInterned == null)
outputBlock.Text += String.Format("{0}', is not interned.", str) + "\n";
else
outputBlock.Text += String.Format("{0}', is interned.", strInterned) + "\n";
}
}
//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.
Version Information
Silverlight
Supported in: 5, 4, 3
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0
XNA Framework
Supported in: Xbox 360, Windows Phone OS 7.0
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.