Export (0) Print
Expand All

String::IsInterned Method

Retrieves a reference to a specified String.

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

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, nullptr.

ExceptionCondition
ArgumentNullException

str is nullptr.

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 by calling the Intern method.

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, nullptr 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.

No code example is currently available or this language may not be supported.
NoteNote

Starting with the .NET Framework version 2.0, you can override the use of the intern pool when you use the Ngen.exe (Native Image Generator) 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 namespace System;
using namespace System::Text;
using namespace 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)];
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. 

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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft