String.IsInterned(String) 方法

定義

擷取對指定 String 的參考。Retrieves a reference to a specified String.

public:
 static System::String ^ IsInterned(System::String ^ str);
public static string IsInterned (string str);
static member IsInterned : string -> string
Public Shared Function IsInterned (str As String) As String

參數

str
String

要在保留集區中搜尋的字串。The string to search for in the intern pool.

傳回

如果是在 Common Language Runtime 保留集區中,則為 str 的參考,否則為 nullA reference to str if it is in the common language runtime intern pool; otherwise, null.

例外狀況

strnullstr is null.

範例

下列範例示範由編譯器自動暫留常值字串。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.

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

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

    Public Shared Sub Test(ByVal sequence As Integer, ByVal 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
End Class

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

備註

Common language runtime 會自動維護一個稱為「實習集區」的資料表,其中包含在程式中宣告的每個唯一常值字串常數的單一實例,以及透過呼叫下列專案,以程式設計方式加入 String 的任何唯一實例Intern 方法。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. 如果您將常值字串常數指派給數個變數,每個變數都會設定為參考 [實習集區] 中的相同常數,而不是參考具有相同值的多個不同 String 實例。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.

這個方法會查閱 [實習集區] 中的 strThis method looks up str in the intern pool. 如果已暫留 str,則會傳回該實例的參考;否則,會傳回 nullIf str has already been interned, a reference to that instance is returned; otherwise, null is returned.

比較這個方法與 Intern 方法。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.

using System;

public class Example
{
   public static void Main()
   {
      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)
            Console.WriteLine("'{0}' is in the string intern pool.", 
                              value);
         else
            Console.WriteLine("'{0}' is not in the string intern pool.",
                              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.
Module Example
   Public Sub Main()
      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
            Console.WriteLine("'{0}' is in the string intern pool.", 
                              value)
         Else
            Console.WriteLine("'{0}' is not in the string intern pool.",
                              value)                      
         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.

注意

從 .NET Framework 版本2.0 開始,當您使用ngen.exe (原生映射產生器)將元件安裝到本機電腦上的原生映射快取時,可以覆寫實習集區的使用。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. 如需詳細資訊,請參閱 Intern 屬性的「備註」一節中的效能考慮。For more information, see Performance Considerations in the Remarks section for the Intern property.

適用於

另請參閱